-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
mgc/terraform-provider-mgc/docs/data-sources/availability_zones.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "mgc_availability_zones Data Source - terraform-provider-mgc" | ||
subcategory: "Availability Zones" | ||
description: |- | ||
List of available regions and availability zones. | ||
--- | ||
|
||
# mgc_availability_zones (Data Source) | ||
|
||
List of available regions and availability zones. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "mgc_availability_zones" "availability_zones" { | ||
} | ||
output "availability_zones" { | ||
value = data.mgc_availability_zones.availability_zones.regions | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Read-Only | ||
|
||
- `regions` (Attributes List) (see [below for nested schema](#nestedatt--regions)) | ||
|
||
<a id="nestedatt--regions"></a> | ||
### Nested Schema for `regions` | ||
|
||
Read-Only: | ||
|
||
- `availability_zones` (Attributes List) (see [below for nested schema](#nestedatt--regions--availability_zones)) | ||
- `region` (String) | ||
|
||
<a id="nestedatt--regions--availability_zones"></a> | ||
### Nested Schema for `regions.availability_zones` | ||
|
||
Optional: | ||
|
||
- `block_type` (String) | ||
|
||
Read-Only: | ||
|
||
- `availability_zone` (String) |
6 changes: 6 additions & 0 deletions
6
mgc/terraform-provider-mgc/examples/data-sources/mgc_availability_zones/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
data "mgc_availability_zones" "availability_zones" { | ||
} | ||
|
||
output "availability_zones" { | ||
value = data.mgc_availability_zones.availability_zones.regions | ||
} |
125 changes: 125 additions & 0 deletions
125
mgc/terraform-provider-mgc/mgc/datasources/mgc_pf_availability_zones.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package datasources | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
mgcSdk "magalu.cloud/lib" | ||
sdkProfileAvailabilityZones "magalu.cloud/lib/products/profile/availability_zones" | ||
"magalu.cloud/terraform-provider-mgc/mgc/client" | ||
) | ||
|
||
var _ datasource.DataSource = &DataSourceAvailabilityZones{} | ||
|
||
type DataSourceAvailabilityZones struct { | ||
sdkClient *mgcSdk.Client | ||
profileAvailZones sdkProfileAvailabilityZones.Service | ||
} | ||
|
||
func NewDataSourceAvailabilityZones() datasource.DataSource { | ||
return &DataSourceAvailabilityZones{} | ||
} | ||
|
||
func (r *DataSourceAvailabilityZones) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_availability_zones" | ||
} | ||
|
||
type Regions struct { | ||
Regions []Region `tfsdk:"regions"` | ||
} | ||
|
||
type Region struct { | ||
Region types.String `tfsdk:"region"` | ||
AvailabilityZones []AvailabilityZones `tfsdk:"availability_zones"` | ||
} | ||
|
||
type AvailabilityZones struct { | ||
AvailabilityZone types.String `tfsdk:"availability_zone"` | ||
BlockType types.String `tfsdk:"block_type"` | ||
} | ||
|
||
func (r *DataSourceAvailabilityZones) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
var err error | ||
var errDetail error | ||
r.sdkClient, err, errDetail = client.NewSDKClient(req, resp) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
err.Error(), | ||
errDetail.Error(), | ||
) | ||
return | ||
} | ||
|
||
r.profileAvailZones = sdkProfileAvailabilityZones.NewService(ctx, r.sdkClient) | ||
} | ||
|
||
func (r *DataSourceAvailabilityZones) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "List of available regions and availability zones.", | ||
Attributes: map[string]schema.Attribute{ | ||
"regions": schema.ListNestedAttribute{ | ||
Computed: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"region": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"availability_zones": schema.ListNestedAttribute{ | ||
Computed: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"availability_zone": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"block_type": schema.StringAttribute{ | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
func (r *DataSourceAvailabilityZones) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data Regions | ||
|
||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
|
||
sdkOutput, err := r.profileAvailZones.ListContext(ctx, sdkProfileAvailabilityZones.ListParameters{}, sdkProfileAvailabilityZones.ListConfigs{}) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Failed to get versions", err.Error()) | ||
return | ||
} | ||
|
||
for _, region := range sdkOutput.Results { | ||
var regionData Region | ||
regionData.Region = types.StringValue(region.RegionId) | ||
for _, az := range region.AvailabilityZones { | ||
regionData.AvailabilityZones = append(regionData.AvailabilityZones, AvailabilityZones{ | ||
AvailabilityZone: types.StringValue(az.AzId), | ||
BlockType: types.StringPointerValue(removeNoneFromString(az.BlockType)), | ||
}) | ||
} | ||
data.Regions = append(data.Regions, regionData) | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func removeNoneFromString(str string) *string { | ||
if str == "none" { | ||
return nil | ||
} | ||
return &str | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,5 +22,8 @@ | |
], | ||
"SSH Keys": [ | ||
"ssh_keys.md" | ||
], | ||
"Availability Zones": [ | ||
"availability_zones.md" | ||
] | ||
} |