Skip to content

Commit

Permalink
tools/importer-rest-api-specs: adding a workaround for Azure/azure-…
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Nov 13, 2023
1 parent 9989aaa commit 05341b9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package dataworkarounds

import (
"fmt"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/pandora/tools/importer-rest-api-specs/models"
)

var _ workaround = workaroundRecoveryServicesSiteRecovery26680{}

// workaroundRecoveryServicesSiteRecovery26680 works around the model `CreateCertificateOptions` being
// present within the API but not being documented in the API Definitions - as such this adds the missing
// model/fields to make this useful.
//
// Swagger PR: https://github.com/Azure/azure-rest-api-specs/pull/26680
type workaroundRecoveryServicesSiteRecovery26680 struct {
}

func (w workaroundRecoveryServicesSiteRecovery26680) IsApplicable(apiDefinition *models.AzureApiDefinition) bool {
if apiDefinition.ServiceName != "RecoveryServicesSiteRecovery" {
return false
}

apiVersions := map[string]struct{}{
"2022-10-01": {},
"2023-01-01": {},
"2023-02-01": {},
"2023-04-01": {},
"2023-06-01": {},
}
if _, apiVersionMatches := apiVersions[apiDefinition.ApiVersion]; !apiVersionMatches {
return false
}

if _, resourceExists := apiVersions["VaultCertificates"]; !resourceExists {
return false
}

return true
}

func (w workaroundRecoveryServicesSiteRecovery26680) Name() string {
return "RecoveryServicesSiteRecovery / 26680"
}

func (w workaroundRecoveryServicesSiteRecovery26680) Process(apiDefinition models.AzureApiDefinition) (*models.AzureApiDefinition, error) {
resource, ok := apiDefinition.Resources["VaultCertificates"]
if !ok {
return nil, fmt.Errorf("expected a Resource named `VaultCertificates` but didn't get one")
}

// 1. Add the missing model
resource.Models["CertificateCreateOptions"] = models.ModelDetails{
Fields: map[string]models.FieldDetails{
"ValidityInHours": {
Required: false,
JsonName: "validityInHours",
ObjectDefinition: &models.ObjectDefinition{
Type: models.ObjectDefinitionInteger,
},
},
},
}

// 2. Add the field referencing the missing model
model, ok := resource.Models["CertificateRequest"]
if !ok {
return nil, fmt.Errorf("expected a Model named `CertificateRequest` but didn't get one")
}
model.Fields["CertificateCreateOptions"] = models.FieldDetails{
Required: false,
ReadOnly: false,
Sensitive: false,
JsonName: "certificateCreateOptions",
Description: "",
CustomFieldType: nil,
ObjectDefinition: &models.ObjectDefinition{
ReferenceName: pointer.To("CertificateCreateOptions"),
Type: models.ObjectDefinitionReference,
},
}
resource.Models["CertificateRequest"] = model

apiDefinition.Resources["VaultCertificates"] = resource
return &apiDefinition, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var workarounds = []workaround{
workaroundRedis22407{},
workaroundMachineLearning25142{},

workaroundRecoveryServicesSiteRecovery26680{},

// @tombuildsstuff: this is an odd place for this however this allows working around inconsistencies in the Swagger
// we should look at moving this into the `resourceids` package when time allows.
workaroundInconsistentlyDefinedSegments{},
Expand Down

0 comments on commit 05341b9

Please sign in to comment.