|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package apimanagement |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/hashicorp/go-azure-helpers/lang/pointer" |
| 12 | + "github.com/hashicorp/go-azure-helpers/lang/response" |
| 13 | + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" |
| 14 | + "github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2024-05-01/policyfragment" |
| 15 | + "github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2024-05-01/workspace" |
| 16 | + "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" |
| 17 | + "github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/schemaz" |
| 18 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" |
| 19 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" |
| 20 | +) |
| 21 | + |
| 22 | +type ApiManagementWorkspacePolicyFragmentModel struct { |
| 23 | + Name string `tfschema:"name"` |
| 24 | + ApiManagementWorkspaceId string `tfschema:"api_management_workspace_id"` |
| 25 | + Description string `tfschema:"description"` |
| 26 | + Format string `tfschema:"format"` |
| 27 | + Value string `tfschema:"value"` |
| 28 | +} |
| 29 | + |
| 30 | +type ApiManagementWorkspacePolicyFragmentResource struct{} |
| 31 | + |
| 32 | +var _ sdk.ResourceWithUpdate = ApiManagementWorkspacePolicyFragmentResource{} |
| 33 | + |
| 34 | +func (r ApiManagementWorkspacePolicyFragmentResource) ResourceType() string { |
| 35 | + return "azurerm_api_management_workspace_policy_fragment" |
| 36 | +} |
| 37 | + |
| 38 | +func (r ApiManagementWorkspacePolicyFragmentResource) ModelObject() interface{} { |
| 39 | + return &ApiManagementWorkspacePolicyFragmentModel{} |
| 40 | +} |
| 41 | + |
| 42 | +func (r ApiManagementWorkspacePolicyFragmentResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { |
| 43 | + return policyfragment.ValidateWorkspacePolicyFragmentID |
| 44 | +} |
| 45 | + |
| 46 | +func (r ApiManagementWorkspacePolicyFragmentResource) Arguments() map[string]*pluginsdk.Schema { |
| 47 | + return map[string]*pluginsdk.Schema{ |
| 48 | + "name": schemaz.SchemaApiManagementChildName(), |
| 49 | + |
| 50 | + "api_management_workspace_id": commonschema.ResourceIDReferenceRequiredForceNew(&workspace.WorkspaceId{}), |
| 51 | + |
| 52 | + "value": { |
| 53 | + Type: pluginsdk.TypeString, |
| 54 | + Required: true, |
| 55 | + DiffSuppressFunc: XmlWhitespaceDiffSuppress, |
| 56 | + }, |
| 57 | + |
| 58 | + "format": { |
| 59 | + Type: pluginsdk.TypeString, |
| 60 | + Optional: true, |
| 61 | + Default: string(policyfragment.PolicyFragmentContentFormatXml), |
| 62 | + ValidateFunc: validation.StringInSlice([]string{ |
| 63 | + string(policyfragment.PolicyFragmentContentFormatRawxml), |
| 64 | + string(policyfragment.PolicyFragmentContentFormatXml), |
| 65 | + }, false), |
| 66 | + }, |
| 67 | + |
| 68 | + "description": { |
| 69 | + Type: pluginsdk.TypeString, |
| 70 | + Optional: true, |
| 71 | + ValidateFunc: validation.StringIsNotEmpty, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (r ApiManagementWorkspacePolicyFragmentResource) Attributes() map[string]*pluginsdk.Schema { |
| 77 | + return map[string]*pluginsdk.Schema{} |
| 78 | +} |
| 79 | + |
| 80 | +func (r ApiManagementWorkspacePolicyFragmentResource) Create() sdk.ResourceFunc { |
| 81 | + return sdk.ResourceFunc{ |
| 82 | + Timeout: 30 * time.Minute, |
| 83 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 84 | + client := metadata.Client.ApiManagement.PolicyFragmentClient_v2024_05_01 |
| 85 | + |
| 86 | + var model ApiManagementWorkspacePolicyFragmentModel |
| 87 | + if err := metadata.Decode(&model); err != nil { |
| 88 | + return fmt.Errorf("decoding: %+v", err) |
| 89 | + } |
| 90 | + |
| 91 | + workspaceId, err := workspace.ParseWorkspaceID(model.ApiManagementWorkspaceId) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + id := policyfragment.NewWorkspacePolicyFragmentID(workspaceId.SubscriptionId, workspaceId.ResourceGroupName, workspaceId.ServiceName, workspaceId.WorkspaceId, model.Name) |
| 97 | + existing, err := client.WorkspacePolicyFragmentGet(ctx, id, policyfragment.WorkspacePolicyFragmentGetOperationOptions{ |
| 98 | + Format: pointer.To(policyfragment.PolicyFragmentContentFormat(model.Format)), |
| 99 | + }) |
| 100 | + if err != nil && !response.WasNotFound(existing.HttpResponse) { |
| 101 | + return fmt.Errorf("checking for presence of existing %s: %+v", id, err) |
| 102 | + } |
| 103 | + |
| 104 | + if !response.WasNotFound(existing.HttpResponse) { |
| 105 | + return metadata.ResourceRequiresImport(r.ResourceType(), id) |
| 106 | + } |
| 107 | + |
| 108 | + parameters := policyfragment.PolicyFragmentContract{ |
| 109 | + Properties: &policyfragment.PolicyFragmentContractProperties{ |
| 110 | + Format: pointer.To(policyfragment.PolicyFragmentContentFormat(model.Format)), |
| 111 | + Value: model.Value, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + if model.Description != "" { |
| 116 | + parameters.Properties.Description = pointer.To(model.Description) |
| 117 | + } |
| 118 | + |
| 119 | + if err := client.WorkspacePolicyFragmentCreateOrUpdateThenPoll(ctx, id, parameters, policyfragment.WorkspacePolicyFragmentCreateOrUpdateOperationOptions{}); err != nil { |
| 120 | + return fmt.Errorf("creating %s: %+v", id, err) |
| 121 | + } |
| 122 | + |
| 123 | + metadata.SetID(id) |
| 124 | + return nil |
| 125 | + }, |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func (r ApiManagementWorkspacePolicyFragmentResource) Update() sdk.ResourceFunc { |
| 130 | + return sdk.ResourceFunc{ |
| 131 | + Timeout: 30 * time.Minute, |
| 132 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 133 | + client := metadata.Client.ApiManagement.PolicyFragmentClient_v2024_05_01 |
| 134 | + |
| 135 | + var model ApiManagementWorkspacePolicyFragmentModel |
| 136 | + if err := metadata.Decode(&model); err != nil { |
| 137 | + return fmt.Errorf("decoding: %+v", err) |
| 138 | + } |
| 139 | + |
| 140 | + id, err := policyfragment.ParseWorkspacePolicyFragmentID(metadata.ResourceData.Id()) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + resp, err := client.WorkspacePolicyFragmentGet(ctx, *id, policyfragment.WorkspacePolicyFragmentGetOperationOptions{ |
| 146 | + Format: pointer.To(policyfragment.PolicyFragmentContentFormat(model.Format)), |
| 147 | + }) |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("retrieving %s: %+v", *id, err) |
| 150 | + } |
| 151 | + |
| 152 | + if resp.Model == nil { |
| 153 | + return fmt.Errorf("retrieving %s: `model` was nil", *id) |
| 154 | + } |
| 155 | + |
| 156 | + if resp.Model.Properties == nil { |
| 157 | + return fmt.Errorf("retrieving %s: `properties` was nil", *id) |
| 158 | + } |
| 159 | + |
| 160 | + payload := resp.Model |
| 161 | + if metadata.ResourceData.HasChange("description") { |
| 162 | + payload.Properties.Description = pointer.To(model.Description) |
| 163 | + } |
| 164 | + |
| 165 | + if metadata.ResourceData.HasChange("value") { |
| 166 | + payload.Properties.Value = model.Value |
| 167 | + } |
| 168 | + |
| 169 | + if metadata.ResourceData.HasChange("format") { |
| 170 | + payload.Properties.Format = pointer.To(policyfragment.PolicyFragmentContentFormat(model.Format)) |
| 171 | + } |
| 172 | + |
| 173 | + if err := client.WorkspacePolicyFragmentCreateOrUpdateThenPoll(ctx, *id, *payload, policyfragment.WorkspacePolicyFragmentCreateOrUpdateOperationOptions{}); err != nil { |
| 174 | + return fmt.Errorf("updating %s: %+v", *id, err) |
| 175 | + } |
| 176 | + |
| 177 | + return nil |
| 178 | + }, |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func (r ApiManagementWorkspacePolicyFragmentResource) Read() sdk.ResourceFunc { |
| 183 | + return sdk.ResourceFunc{ |
| 184 | + Timeout: 5 * time.Minute, |
| 185 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 186 | + client := metadata.Client.ApiManagement.PolicyFragmentClient_v2024_05_01 |
| 187 | + |
| 188 | + id, err := policyfragment.ParseWorkspacePolicyFragmentID(metadata.ResourceData.Id()) |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + |
| 193 | + format := metadata.ResourceData.Get("format").(string) |
| 194 | + if format == "" { |
| 195 | + format = string(policyfragment.PolicyFragmentContentFormatXml) |
| 196 | + } |
| 197 | + resp, err := client.WorkspacePolicyFragmentGet(ctx, *id, policyfragment.WorkspacePolicyFragmentGetOperationOptions{ |
| 198 | + Format: pointer.To(policyfragment.PolicyFragmentContentFormat(format)), |
| 199 | + }) |
| 200 | + if err != nil { |
| 201 | + if response.WasNotFound(resp.HttpResponse) { |
| 202 | + return metadata.MarkAsGone(*id) |
| 203 | + } |
| 204 | + |
| 205 | + return fmt.Errorf("retrieving %s: %+v", *id, err) |
| 206 | + } |
| 207 | + |
| 208 | + state := ApiManagementWorkspacePolicyFragmentModel{ |
| 209 | + Name: id.PolicyFragmentName, |
| 210 | + ApiManagementWorkspaceId: workspace.NewWorkspaceID(id.SubscriptionId, id.ResourceGroupName, id.ServiceName, id.WorkspaceId).ID(), |
| 211 | + } |
| 212 | + |
| 213 | + if model := resp.Model; model != nil { |
| 214 | + if props := model.Properties; props != nil { |
| 215 | + state.Description = pointer.From(props.Description) |
| 216 | + state.Value = props.Value |
| 217 | + |
| 218 | + // The API only returns `format` when set to "rawxml"; the default "xml" is intentionally never returned. |
| 219 | + format := policyfragment.PolicyFragmentContentFormatXml |
| 220 | + if props.Format != nil { |
| 221 | + format = pointer.From(props.Format) |
| 222 | + } |
| 223 | + state.Format = string(format) |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + return metadata.Encode(&state) |
| 228 | + }, |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +func (r ApiManagementWorkspacePolicyFragmentResource) Delete() sdk.ResourceFunc { |
| 233 | + return sdk.ResourceFunc{ |
| 234 | + Timeout: 30 * time.Minute, |
| 235 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 236 | + client := metadata.Client.ApiManagement.PolicyFragmentClient_v2024_05_01 |
| 237 | + |
| 238 | + id, err := policyfragment.ParseWorkspacePolicyFragmentID(metadata.ResourceData.Id()) |
| 239 | + if err != nil { |
| 240 | + return err |
| 241 | + } |
| 242 | + |
| 243 | + if _, err := client.WorkspacePolicyFragmentDelete(ctx, *id, policyfragment.WorkspacePolicyFragmentDeleteOperationOptions{}); err != nil { |
| 244 | + return fmt.Errorf("deleting %s: %+v", *id, err) |
| 245 | + } |
| 246 | + |
| 247 | + return nil |
| 248 | + }, |
| 249 | + } |
| 250 | +} |
0 commit comments