-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
custom_location_data_source.go
138 lines (112 loc) · 4.21 KB
/
custom_location_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package extendedlocation
import (
"context"
"fmt"
"time"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15/customlocations"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)
type CustomLocationDataSource struct{}
var _ sdk.DataSource = CustomLocationDataSource{}
type CustomLocationDataSourceModel struct {
ClusterExtensionIds []string `tfschema:"cluster_extension_ids"`
DisplayName string `tfschema:"display_name"`
HostResourceId string `tfschema:"host_resource_id"`
Identities []identity.ModelSystemAssigned `tfschema:"identities"`
Location string `tfschema:"location"`
Name string `tfschema:"name"`
Namespace string `tfschema:"namespace"`
ResourceGroupName string `tfschema:"resource_group_name"`
Tags map[string]interface{} `tfschema:"tags"`
}
func (r CustomLocationDataSource) ResourceType() string {
return "azurerm_custom_location"
}
func (r CustomLocationDataSource) ModelObject() interface{} {
return &CustomLocationDataSourceModel{}
}
func (r CustomLocationDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"resource_group_name": commonschema.ResourceGroupNameForDataSource(),
}
}
func (r CustomLocationDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"cluster_extension_ids": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"display_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"host_resource_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"identities": commonschema.SystemAssignedIdentityComputed(),
"location": {
Type: pluginsdk.TypeString,
Computed: true,
},
"namespace": {
Type: pluginsdk.TypeString,
Computed: true,
},
"tags": commonschema.TagsDataSource(),
}
}
func (r CustomLocationDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.ExtendedLocation.CustomLocations
subscriptionId := metadata.Client.Account.SubscriptionId
var model CustomLocationDataSourceModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}
id := customlocations.NewCustomLocationID(subscriptionId, model.ResourceGroupName, model.Name)
existing, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("%s does not exist", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}
state := CustomLocationDataSourceModel{
Name: model.Name,
ResourceGroupName: model.ResourceGroupName,
}
if model := existing.Model; model != nil {
state.Location = location.Normalize(model.Location)
state.Tags = tags.Flatten(model.Tags)
state.Identities = identity.FlattenSystemAssignedToModel(model.Identity)
if props := model.Properties; props != nil {
state.ClusterExtensionIds = pointer.From(props.ClusterExtensionIds)
state.DisplayName = pointer.From(props.DisplayName)
state.HostResourceId = pointer.From(props.HostResourceId)
state.Namespace = pointer.From(props.Namespace)
}
}
metadata.SetID(id)
return metadata.Encode(&state)
},
}
}