|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package devcenter |
| 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-helpers/resourcemanager/location" |
| 15 | + "github.com/hashicorp/go-azure-sdk/resource-manager/devcenter/2025-02-01/devboxdefinitions" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 17 | + "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" |
| 18 | + "github.com/hashicorp/terraform-provider-azurerm/internal/services/devcenter/validate" |
| 19 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" |
| 20 | +) |
| 21 | + |
| 22 | +var _ sdk.DataSource = DevCenterDevBoxDefinitionDataSource{} |
| 23 | + |
| 24 | +type DevCenterDevBoxDefinitionDataSource struct{} |
| 25 | + |
| 26 | +type DevCenterDevBoxDefinitionDataSourceModel struct { |
| 27 | + Name string `tfschema:"name"` |
| 28 | + Location string `tfschema:"location"` |
| 29 | + DevCenterId string `tfschema:"dev_center_id"` |
| 30 | + ImageReferenceId string `tfschema:"image_reference_id"` |
| 31 | + SkuName string `tfschema:"sku_name"` |
| 32 | + Tags map[string]string `tfschema:"tags"` |
| 33 | +} |
| 34 | + |
| 35 | +func (DevCenterDevBoxDefinitionDataSource) Arguments() map[string]*pluginsdk.Schema { |
| 36 | + return map[string]*pluginsdk.Schema{ |
| 37 | + "name": { |
| 38 | + Type: pluginsdk.TypeString, |
| 39 | + Required: true, |
| 40 | + ValidateFunc: validate.DevCenterDevBoxDefinitionName, |
| 41 | + }, |
| 42 | + |
| 43 | + "dev_center_id": commonschema.ResourceIDReferenceRequired(&devboxdefinitions.DevCenterId{}), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (DevCenterDevBoxDefinitionDataSource) Attributes() map[string]*pluginsdk.Schema { |
| 48 | + return map[string]*pluginsdk.Schema{ |
| 49 | + "image_reference_id": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + |
| 54 | + "location": commonschema.LocationComputed(), |
| 55 | + |
| 56 | + "sku_name": { |
| 57 | + Type: pluginsdk.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + |
| 61 | + "tags": commonschema.TagsDataSource(), |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (DevCenterDevBoxDefinitionDataSource) ModelObject() interface{} { |
| 66 | + return &DevCenterDevBoxDefinitionDataSourceModel{} |
| 67 | +} |
| 68 | + |
| 69 | +func (DevCenterDevBoxDefinitionDataSource) ResourceType() string { |
| 70 | + return "azurerm_dev_center_dev_box_definition" |
| 71 | +} |
| 72 | + |
| 73 | +func (r DevCenterDevBoxDefinitionDataSource) Read() sdk.ResourceFunc { |
| 74 | + return sdk.ResourceFunc{ |
| 75 | + Timeout: 5 * time.Minute, |
| 76 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 77 | + client := metadata.Client.DevCenter.V20250201.DevBoxDefinitions |
| 78 | + subscriptionId := metadata.Client.Account.SubscriptionId |
| 79 | + |
| 80 | + var state DevCenterDevBoxDefinitionDataSourceModel |
| 81 | + if err := metadata.Decode(&state); err != nil { |
| 82 | + return fmt.Errorf("decoding: %+v", err) |
| 83 | + } |
| 84 | + |
| 85 | + devCenterId, err := devboxdefinitions.ParseDevCenterID(state.DevCenterId) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + id := devboxdefinitions.NewDevCenterDevBoxDefinitionID(subscriptionId, devCenterId.ResourceGroupName, devCenterId.DevCenterName, state.Name) |
| 91 | + |
| 92 | + resp, err := client.Get(ctx, id) |
| 93 | + if err != nil { |
| 94 | + if response.WasNotFound(resp.HttpResponse) { |
| 95 | + return fmt.Errorf("%s was not found", id) |
| 96 | + } |
| 97 | + |
| 98 | + return fmt.Errorf("retrieving %s: %+v", id, err) |
| 99 | + } |
| 100 | + |
| 101 | + metadata.SetID(id) |
| 102 | + |
| 103 | + state.Name = id.DevBoxDefinitionName |
| 104 | + state.DevCenterId = devboxdefinitions.NewDevCenterID(id.SubscriptionId, id.ResourceGroupName, id.DevCenterName).ID() |
| 105 | + |
| 106 | + if model := resp.Model; model != nil { |
| 107 | + state.Location = location.Normalize(model.Location) |
| 108 | + state.Tags = pointer.From(model.Tags) |
| 109 | + |
| 110 | + if props := model.Properties; props != nil { |
| 111 | + if v := props.ImageReference; v != nil { |
| 112 | + state.ImageReferenceId = pointer.From(v.Id) |
| 113 | + } |
| 114 | + |
| 115 | + if v := props.Sku; v != nil { |
| 116 | + state.SkuName = v.Name |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return metadata.Encode(&state) |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
0 commit comments