|
| 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 = workaroundBotService27351{} |
| 11 | + |
| 12 | +type workaroundBotService27351 struct { |
| 13 | +} |
| 14 | + |
| 15 | +func (workaroundBotService27351) IsApplicable(apiDefinition *models.AzureApiDefinition) bool { |
| 16 | + // This workaround fixes an issue where the BotService Channel URI is defined using two subtly different Resource IDs. |
| 17 | + // Fix: https://github.com/Azure/azure-rest-api-specs/pull/27351 |
| 18 | + // |
| 19 | + // The DELETE and GET define the Uri Parameter `channelName` as a String - whereas the PATCH and POST define it as |
| 20 | + // a Constant. |
| 21 | + // The result of this is that whilst we switch out the Resource ID for a Common ID, this inconsistency means we end up |
| 22 | + // with 2 different Resource IDs in the output, the Common ID (`BotServiceChannelId`) using a Constant Segment and another |
| 23 | + // Resource ID (`ChannelId`) using a String segment. |
| 24 | + // |
| 25 | + // As such this workaround fixes this issue by normalizing the resulting output - since these are the same endpoint/should |
| 26 | + // support the same values for this URI Segment. |
| 27 | + serviceMatches := apiDefinition.ServiceName == "BotService" |
| 28 | + apiVersions := map[string]struct{}{ |
| 29 | + "2021-05-01-preview": {}, |
| 30 | + "2022-06-15-preview": {}, |
| 31 | + "2023-09-15-preview": {}, |
| 32 | + "2020-06-02": {}, |
| 33 | + "2021-03-01": {}, |
| 34 | + "2022-09-15": {}, |
| 35 | + } |
| 36 | + _, apiVersionMatches := apiVersions[apiDefinition.ApiVersion] |
| 37 | + return serviceMatches && apiVersionMatches |
| 38 | +} |
| 39 | + |
| 40 | +func (workaroundBotService27351) Name() string { |
| 41 | + return "BotService / 27351" |
| 42 | +} |
| 43 | + |
| 44 | +func (workaroundBotService27351) Process(input models.AzureApiDefinition) (*models.AzureApiDefinition, error) { |
| 45 | + output := input |
| 46 | + |
| 47 | + resource, ok := output.Resources["Channel"] |
| 48 | + if !ok { |
| 49 | + return nil, fmt.Errorf("expected a Resource named `Channel` but didn't get one") |
| 50 | + } |
| 51 | + // ensure we've got the Resource ID we're going to switch over to |
| 52 | + if _, ok := resource.ResourceIds["BotServiceChannelId"]; !ok { |
| 53 | + return nil, fmt.Errorf("expected a Resource ID named `BotServiceChannelId` but didn't get one") |
| 54 | + } |
| 55 | + |
| 56 | + // ensure we've got the mismatched Resource ID in order to remove it |
| 57 | + if _, ok := resource.ResourceIds["ChannelId"]; !ok { |
| 58 | + return nil, fmt.Errorf("expected a Resource ID named `ChannelId` but didn't get one") |
| 59 | + } |
| 60 | + delete(resource.ResourceIds, "ChannelId") |
| 61 | + |
| 62 | + for operationName, operation := range resource.Operations { |
| 63 | + if operation.ResourceIdName != nil && *operation.ResourceIdName == "ChannelId" { |
| 64 | + operation.ResourceIdName = pointer.To("BotServiceChannelId") |
| 65 | + } |
| 66 | + resource.Operations[operationName] = operation |
| 67 | + } |
| 68 | + |
| 69 | + output.Resources["Channel"] = resource |
| 70 | + |
| 71 | + return &output, nil |
| 72 | +} |
0 commit comments