Skip to content

Commit

Permalink
CXF-106630: Fix typing errors for GeoCoordinates and DataSource name
Browse files Browse the repository at this point in the history
  • Loading branch information
d-bhola committed Jan 17, 2025
1 parent 95b604d commit 408786b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 166 deletions.
43 changes: 5 additions & 38 deletions internal/resources/fabric/metros/datasources_metro_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

func NewDataSourceMetroCode() datasource.DataSource {
return &DataSource{
return &DataSourceMetroCode{
BaseDataSource: framework.NewBaseDataSource(
framework.BaseDataSourceConfig{
Name: "equinix_fabric_metro_code",
Name: "equinix_fabric_metro",
},
),
}
Expand All @@ -22,57 +22,26 @@ type DataSourceMetroCode struct {
framework.BaseDataSource
}

func (r *DataSource) Schema(
func (r *DataSourceMetroCode) Schema(
ctx context.Context,
req datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = dataSourceSingleMetroSchema(ctx)
}

//func (d *DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
// resp.Schema = schema.Schema{
// Attributes: map[string]schema.Attribute{
// "example_attribute": schema.StringAttribute{
// Required: true,
// },
// "id": schema.StringAttribute{
// Computed: true,
// },
// },
// }
//}

//
//func (r *DataSource) Schema(ctx context.Context, request SchemaRequest, response SchemaResponse) {
// response.Schema = dataSourceSingleMetroSchema(ctx)
//}

/*func (r *DataSource) AllMetrosSchema(ctx context.Context, response *datasource.SchemaResponse) {
response.Schema = dataSourceAllMetroSchema(ctx)
}
func (r *DataSource) MetroCodeSchema(ctx context.Context, response *datasource.SchemaResponse) {
response.Schema = dataSourceSingleMetroSchema(ctx)
}*/

// READ function for GET Metro Code data source
func (r *DataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
func (r *DataSourceMetroCode) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
client := r.Meta.NewFabricClientForFramework(ctx, request.ProviderMeta)

// Retrieve values from plan
var data MetroModel
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

// Extract the ID of the resource from the config
//id := data.ID.ValueString()

metroCode := data.Code.ValueString()
metroCode := data.MetroCode.ValueString()

// Use API client to get the current state of the resource
metroByCode, _, err := client.MetrosApi.GetMetroByCode(ctx, metroCode).Execute()

if err != nil {
Expand All @@ -81,12 +50,10 @@ func (r *DataSource) Read(ctx context.Context, request datasource.ReadRequest, r
return
}

// Set state to fully populated data
response.Diagnostics.Append(data.parseDataSourceByMetroCode(ctx, metroByCode)...)
if response.Diagnostics.HasError() {
return
}

// Update the Terraform state
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}
86 changes: 0 additions & 86 deletions internal/resources/fabric/metros/datasources_metros.go
Original file line number Diff line number Diff line change
@@ -1,87 +1 @@
package metros

import (
"github.com/equinix/terraform-provider-equinix/internal/framework"
"github.com/hashicorp/terraform-plugin-framework/datasource"
)

func NewDataSourceMetros() datasource.DataSource {
return &DataSource{
BaseDataSource: framework.NewBaseDataSource(
framework.BaseDataSourceConfig{
Name: "equinix_fabric_metro_code",
},
),
}
}

type DataSource struct {
framework.BaseDataSource
}

//
//func (r *DataSource) Schema(
// ctx context.Context,
// req datasource.SchemaRequest,
// resp *datasource.SchemaResponse,
//) {
// resp.Schema = dataSourceAllMetroSchema(ctx)
//}

//func (r *DataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
// //TODO implement me
// panic("implement me")
//}
//
//func (r *DataSource) Schema(
// ctx context.Context,
// req datasource.SchemaRequest,
// resp *datasource.SchemaResponse,
//) {
// resp.Schema = dataSourceAllMetroSchema(ctx)
//}

//READ function for the Get Metros data source also, use the Get Metros API

//func (r *DataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
// client := r.Meta.NewFabricClientForFramework(ctx, request.ProviderMeta)
//
// // Retrieve values from plan
// var data AllMetrosModel
// response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
// if response.Diagnostics.HasError() {
// return
// }
//
// // Extract the ID of the resource from the config
// id := data.ID.ValueString()
//
// // Use API client to get the current state of the resource
// metros, _, err := client.MetrosApi.GetMetros(ctx).Execute()
//
// if err != nil {
// // If Metros is not found, remove it from the state
// if equinix_errors.IsNotFound(err) {
// response.Diagnostics.AddWarning(
// "Get All Metros",
// fmt.Sprintf("[WARN] Metros (%s) not found, removing from state", id),
// )
// response.State.RemoveResource(ctx)
// return
// }
// response.Diagnostics.AddError(
// "Error reading - Get All Metros",
// "Could not read Metros with ID:"+id+": "+err.Error(),
// )
// return
// }
//
// // Set state to fully populated data
// response.Diagnostics.Append(data.parse(ctx, metros)...)
// if response.Diagnostics.HasError() {
// return
// }
//
// // Update the Terraform state
// response.Diagnostics.Append(response.State.Set(ctx, &data)...)
//}
12 changes: 11 additions & 1 deletion internal/resources/fabric/metros/datasources_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,17 @@ func getMetroSchema(ctx context.Context) map[string]schema.Attribute {
"geo_coordinates": schema.SingleNestedAttribute{
Description: "Geographic location data of Fabric Metro",
CustomType: fwtypes.NewObjectTypeOf[GeoCoordinatesModel](ctx),
Computed: true,
Attributes: map[string]schema.Attribute{
"latitude": schema.Float64Attribute{
Description: "Latitude of the Metro",
Computed: true,
},
"longitude": schema.Float64Attribute{
Description: "Longitude of the Metro",
Computed: true,
},
},
Computed: true,
},
"connected_metros": schema.ListAttribute{
Description: "Arrays of objects containing latency data for the specified metros",
Expand Down
81 changes: 40 additions & 41 deletions internal/resources/fabric/metros/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MetroModel struct {
Href types.String `tfsdk:"href"`
Type types.String `tfsdk:"type"`
Code types.String `tfsdk:"code"`
MetroCode types.String `tfsdk:"metro_code"`
Region types.String `tfsdk:"region"`
Name types.String `tfsdk:"name"`
EquinixASN types.Int64 `tfsdk:"equinix_asn"`
Expand Down Expand Up @@ -52,14 +53,11 @@ type AllMetrosModel struct {
}

func (m *MetroModel) parseDataSourceByMetroCode(ctx context.Context, metro *fabricv4.Metro) diag.Diagnostics {
diags := parseMetros(ctx, metro,
&m.ID, &m.Type, &m.Href, &m.Code, &m.Region, &m.Name,
&m.EquinixASN, &m.LocalVCBandwidthMax, &m.GeoCoordinates, &m.ConnectedMetros, &m.GeoScopes)

diags := parseMetros(ctx, metro, &m.Type, &m.Href, &m.Code, &m.Region, &m.Name, &m.EquinixASN, &m.LocalVCBandwidthMax, &m.GeoCoordinates, &m.ConnectedMetros, &m.GeoScopes)
return diags
}

func parseMetros(ctx context.Context, metro *fabricv4.Metro, id, tp, href, code, region, name *basetypes.StringValue, equinixAsn, localBandwidthMax *basetypes.Int64Value, geoCoordinates *fwtypes.ObjectValueOf[GeoCoordinatesModel], connectedMetros *fwtypes.ListNestedObjectValueOf[ConnectedMetroModel], gScopes *fwtypes.ListValueOf[types.String]) diag.Diagnostics {
func parseMetros(ctx context.Context, metro *fabricv4.Metro, tp, href, code, region, name *basetypes.StringValue, equinixAsn, localBandwidthMax *basetypes.Int64Value, geoCoordinates *fwtypes.ObjectValueOf[GeoCoordinatesModel], connectedMetros *fwtypes.ListNestedObjectValueOf[ConnectedMetroModel], gScopes *fwtypes.ListValueOf[types.String]) diag.Diagnostics {

var diags diag.Diagnostics
*href = types.StringValue(metro.GetHref())
Expand All @@ -79,57 +77,24 @@ func parseMetros(ctx context.Context, metro *fabricv4.Metro, id, tp, href, code,
}

geoCoord, diags := parseGeoCoordinates(ctx, metro.GetGeoCoordinates())

if diags.HasError() {
return diags
}

*geoCoordinates = geoCoord

connMetros, diags := parseconnectedMetros(ctx, metro.GetConnectedMetros())
connMetros, diags := parseConnectedMetros(ctx, metro.GetConnectedMetros())
if diags.HasError() {
return diags
}
*connectedMetros = connMetros

geoScopes, diags := parseGeoScopes(ctx, metro.GetGeoScopes())
if diags.HasError() {
return diags
}
*gScopes = geoScopes
return diags
}

func parseconnectedMetros(ctx context.Context, connectedMetros []fabricv4.ConnectedMetro) (fwtypes.ListNestedObjectValueOf[ConnectedMetroModel], diag.Diagnostics) {
connMetros := make([]ConnectedMetroModel, len(connectedMetros))
for i, metro := range connectedMetros {
connMetros[i] = ConnectedMetroModel{
Href: types.StringValue(*metro.Href),
Code: types.StringValue(*metro.Code),
AvgLatency: types.Float32Value(*metro.AvgLatency),
RemoteVCBandwidthMax: types.Int64Value(*metro.RemoteVCBandwidthMax),
}
}
return fwtypes.NewListNestedObjectValueOfValueSlice(ctx, connMetros), nil
}

func parseGeoCoordinates(ctx context.Context, coordinates fabricv4.GeoCoordinates) (fwtypes.ObjectValueOf[GeoCoordinatesModel], diag.Diagnostics) {
var diags diag.Diagnostics
//if coordinates == nil {
// diags.AddError("Invalid Input", "Coordinates should not be nil")
// return nil, diags
//}
//
//if coordinates.Latitude == nil || coordinates.Longitude == nil {
// diags.AddError("Invalid Input", "Latitude and Longitude should not be nil")
// return nil, diags
//}

result := GeoCoordinatesModel{
Latitude: types.Float64Value(*coordinates.Latitude),
Longitude: types.Float64Value(*coordinates.Longitude),
}

return fwtypes.NewObjectValueOf[GeoCoordinatesModel](ctx, &result), diags
return diags
}

func parseGeoScopes(ctx context.Context, scopes []fabricv4.GeoScopeType) (fwtypes.ListValueOf[types.String], diag.Diagnostics) {
Expand All @@ -146,3 +111,37 @@ func parseGeoScopes(ctx context.Context, scopes []fabricv4.GeoScopeType) (fwtype
}
return geoScopeValue, diags
}

func parseGeoCoordinates(ctx context.Context, coordinates fabricv4.GeoCoordinates) (fwtypes.ObjectValueOf[GeoCoordinatesModel], diag.Diagnostics) {
diags := diag.Diagnostics{}

result := GeoCoordinatesModel{}
if coordinates.Latitude != nil {
result.Latitude = types.Float64Value(coordinates.GetLatitude())
}

if coordinates.Longitude != nil {
result.Longitude = types.Float64Value(coordinates.GetLongitude())
}
return fwtypes.NewObjectValueOf[GeoCoordinatesModel](ctx, &result), diags
}

func parseConnectedMetros(ctx context.Context, connectedMetros []fabricv4.ConnectedMetro) (fwtypes.ListNestedObjectValueOf[ConnectedMetroModel], diag.Diagnostics) {
connMetros := make([]ConnectedMetroModel, len(connectedMetros))
for i, metro := range connectedMetros {
connMetros[i] = ConnectedMetroModel{}
if metro.Href != nil {
connMetros[i].Href = types.StringValue(metro.GetHref())
}
if metro.Code != nil {
connMetros[i].Code = types.StringValue(metro.GetCode())
}
if metro.AvgLatency != nil {
connMetros[i].AvgLatency = types.Float32Value(metro.GetAvgLatency())
}
if metro.RemoteVCBandwidthMax != nil {
connMetros[i].RemoteVCBandwidthMax = types.Int64Value(metro.GetRemoteVCBandwidthMax())
}
}
return fwtypes.NewListNestedObjectValueOfValueSlice(ctx, connMetros), nil
}

0 comments on commit 408786b

Please sign in to comment.