|
| 1 | +package dataworkarounds |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/go-azure-helpers/lang/pointer" |
| 7 | + "github.com/hashicorp/pandora/tools/importer-rest-api-specs/models" |
| 8 | +) |
| 9 | + |
| 10 | +var _ workaround = workaroundDataFactory23013{} |
| 11 | + |
| 12 | +// workaroundDataFactory23013 works around the `IntegrationRuntimeReference` and `LinkedServiceReference` models both |
| 13 | +// defining a `type` field indicating that these are discriminated types but not defined as a Discriminated Type |
| 14 | +// Swagger PR: https://github.com/Azure/azure-rest-api-specs/pull/23013 |
| 15 | +type workaroundDataFactory23013 struct{} |
| 16 | + |
| 17 | +func (workaroundDataFactory23013) IsApplicable(apiDefinition *models.AzureApiDefinition) bool { |
| 18 | + return apiDefinition.ServiceName == "DataFactory" && apiDefinition.ApiVersion == "2018-06-01" |
| 19 | +} |
| 20 | + |
| 21 | +func (workaroundDataFactory23013) Name() string { |
| 22 | + return "DataFactory / 23013" |
| 23 | +} |
| 24 | + |
| 25 | +func (workaroundDataFactory23013) Process(apiDefinition models.AzureApiDefinition) (*models.AzureApiDefinition, error) { |
| 26 | + resource, ok := apiDefinition.Resources["DataFlowDebugSession"] |
| 27 | + if !ok { |
| 28 | + return nil, fmt.Errorf("couldn't find API Resource DataFlowDebugSession") |
| 29 | + } |
| 30 | + |
| 31 | + // add the new discriminated parent type |
| 32 | + resource.Models["Reference"] = models.ModelDetails{ |
| 33 | + TypeHintIn: pointer.To("Type"), |
| 34 | + Fields: map[string]models.FieldDetails{ |
| 35 | + "Type": { |
| 36 | + ObjectDefinition: &models.ObjectDefinition{ |
| 37 | + Type: models.ObjectDefinitionString, |
| 38 | + }, |
| 39 | + Required: true, |
| 40 | + JsonName: "type", |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + // update the existing models to be discriminated types and remove the `type` field from them |
| 46 | + // ` and `LinkedServiceReference |
| 47 | + modelNames := []string{ |
| 48 | + "IntegrationRuntimeReference", |
| 49 | + "LinkedServiceReference", |
| 50 | + } |
| 51 | + for _, modelName := range modelNames { |
| 52 | + model, ok := resource.Models[modelName] |
| 53 | + if !ok { |
| 54 | + return nil, fmt.Errorf("couldn't find model %q", modelName) |
| 55 | + } |
| 56 | + delete(model.Fields, "Type") |
| 57 | + model.ParentTypeName = pointer.To("Reference") |
| 58 | + model.TypeHintValue = pointer.To(modelName) |
| 59 | + resource.Models[modelName] = model |
| 60 | + } |
| 61 | + |
| 62 | + // delete the now unused `Type` constant |
| 63 | + delete(resource.Constants, "Type") |
| 64 | + |
| 65 | + apiDefinition.Resources["DataFlowDebugSession"] = resource |
| 66 | + return &apiDefinition, nil |
| 67 | +} |
0 commit comments