Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools/importer-rest-api-specs: flagging fields that are expected to be Computed in the Terraform Schema to be ReadOnly for now #3899

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,109 @@ type workaroundTempReadOnlyFields struct {
}

func (w workaroundTempReadOnlyFields) IsApplicable(apiDefinition *models.AzureApiDefinition) bool {
return apiDefinition.ServiceName == "ManagedIdentity" && apiDefinition.ApiVersion == "2023-01-31"
if apiDefinition.ServiceName == "ContainerService" && apiDefinition.ApiVersion == "2022-09-02-preview" {
return true
}

if apiDefinition.ServiceName == "DevCenter" && apiDefinition.ApiVersion == "2023-04-01" {
return true
}

if apiDefinition.ServiceName == "LoadTestService" && apiDefinition.ApiVersion == "2022-12-01" {
return true
}

if apiDefinition.ServiceName == "ManagedIdentity" && apiDefinition.ApiVersion == "2023-01-31" {
return true
}

return false
}

func (w workaroundTempReadOnlyFields) Name() string {
return "Temp - Mark readonly fields as readonly"
}

func (w workaroundTempReadOnlyFields) Process(apiDefinition models.AzureApiDefinition) (*models.AzureApiDefinition, error) {
resource, ok := apiDefinition.Resources["ManagedIdentities"]
if !ok {
return nil, fmt.Errorf("expected an APIResource `ManagedIdentities` but didn't get one")
if apiDefinition.ServiceName == "ContainerService" && apiDefinition.ApiVersion == "2022-09-02-preview" {
definition, err := w.markFieldAsComputed(apiDefinition, "Fleets", "FleetHubProfile", "Fqdn")
if err != nil {
return nil, err
}

definition, err = w.markFieldAsComputed(*definition, "Fleets", "FleetHubProfile", "KubernetesVersion")
if err != nil {
return nil, err
}

return definition, nil
}

model, ok := resource.Models["UserAssignedIdentityProperties"]
if !ok {
return nil, fmt.Errorf("expected a Model `UserAssignedIdentityProperties` but didn't get one")
if apiDefinition.ServiceName == "DevCenter" && apiDefinition.ApiVersion == "2023-04-01" {
definition, err := w.markFieldAsComputed(apiDefinition, "Projects", "ProjectProperties", "DevCenterUri")
if err != nil {
return nil, err
}

definition, err = w.markFieldAsComputed(*definition, "DevCenters", "DevCenterProperties", "DevCenterUri")
if err != nil {
return nil, err
}

return definition, nil
}

clientId, ok := model.Fields["ClientId"]
if !ok {
return nil, fmt.Errorf("expected a Field `ClientId` but didn't get one")
if apiDefinition.ServiceName == "LoadTestService" && apiDefinition.ApiVersion == "2022-12-01" {
definition, err := w.markFieldAsComputed(apiDefinition, "LoadTests", "LoadTestProperties", "DataPlaneURI")
if err != nil {
return nil, err
}

return definition, nil
}
clientId.Optional = true
clientId.ReadOnly = true
model.Fields["ClientId"] = clientId

principalId, ok := model.Fields["PrincipalId"]
if !ok {
return nil, fmt.Errorf("expected a Field `PrincipalId` but didn't get one")
if apiDefinition.ServiceName == "ManagedIdentity" && apiDefinition.ApiVersion == "2023-01-31" {
definition, err := w.markFieldAsComputed(apiDefinition, "ManagedIdentities", "UserAssignedIdentityProperties", "ClientId")
if err != nil {
return nil, err
}

definition, err = w.markFieldAsComputed(*definition, "ManagedIdentities", "UserAssignedIdentityProperties", "PrincipalId")
if err != nil {
return nil, err
}

definition, err = w.markFieldAsComputed(*definition, "ManagedIdentities", "UserAssignedIdentityProperties", "TenantId")
if err != nil {
return nil, err
}

return definition, nil
}
principalId.Optional = true
principalId.ReadOnly = true
model.Fields["PrincipalId"] = principalId

tenantId, ok := model.Fields["TenantId"]
return nil, fmt.Errorf("unexpected Service %q / API Version %q", apiDefinition.ServiceName, apiDefinition.ApiVersion)
}

func (w workaroundTempReadOnlyFields) markFieldAsComputed(input models.AzureApiDefinition, apiResourceName, modelName, fieldName string) (*models.AzureApiDefinition, error) {
resource, ok := input.Resources[apiResourceName]
if !ok {
return nil, fmt.Errorf("expected a Field `TenantId` but didn't get one")
return nil, fmt.Errorf("expected an APIResource %q but didn't get one", apiResourceName)
}
tenantId.Optional = true
tenantId.ReadOnly = true
model.Fields["TenantId"] = tenantId

resource.Models["UserAssignedIdentityProperties"] = model
model, ok := resource.Models[modelName]
if !ok {
return nil, fmt.Errorf("expected a Model %q but didn't get one", modelName)
}
field, ok := model.Fields[fieldName]
if !ok {
return nil, fmt.Errorf("expected a Field %q but didn't get one", fieldName)
}
field.Optional = false
field.ReadOnly = true
field.Required = false
field.Sensitive = false
model.Fields[fieldName] = field

apiDefinition.Resources["ManagedIdentities"] = resource
return &apiDefinition, nil
resource.Models[modelName] = model
input.Resources[apiResourceName] = resource
return &input, nil
}
Loading