forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spatial_anchors_account_resource.go
166 lines (135 loc) · 5.23 KB
/
spatial_anchors_account_resource.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package mixedreality
import (
"fmt"
"regexp"
"time"
"github.com/Azure/azure-sdk-for-go/services/mixedreality/mgmt/2021-01-01/mixedreality"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/tf"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/mixedreality/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tags"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func resourceSpatialAnchorsAccount() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceSpatialAnchorsAccountCreateUpdate,
Read: resourceSpatialAnchorsAccountRead,
Update: resourceSpatialAnchorsAccountCreateUpdate,
Delete: resourceSpatialAnchorsAccountDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.SpatialAnchorsAccountID(id)
return err
}),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[-\w._()]+$`),
"Spatial Anchors Account name must be 1 - 90 characters long, contain only word characters and underscores.",
),
},
"location": azure.SchemaLocation(),
"resource_group_name": azure.SchemaResourceGroupName(),
"account_domain": {
Type: pluginsdk.TypeString,
Computed: true,
},
"account_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"tags": tags.Schema(),
},
}
}
func resourceSpatialAnchorsAccountCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MixedReality.SpatialAnchorsAccountClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
name := d.Get("name").(string)
location := azure.NormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)
t := d.Get("tags").(map[string]interface{})
if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Spatial Anchors Account %q (Resource Group %q): %s", name, resourceGroup, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_spatial_anchors_account", *existing.ID)
}
}
account := mixedreality.SpatialAnchorsAccount{
Location: &location,
Tags: tags.Expand(t),
}
if _, err := client.Create(ctx, resourceGroup, name, account); err != nil {
return fmt.Errorf("creating/updating Spatial Anchors Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("retrieving Spatial Anchors Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if resp.ID == nil {
return fmt.Errorf("cannot read Spatial Anchors Account %q (Resource Group %q) ID", name, resourceGroup)
}
d.SetId(*resp.ID)
return resourceSpatialAnchorsAccountRead(d, meta)
}
func resourceSpatialAnchorsAccountRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MixedReality.SpatialAnchorsAccountClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.SpatialAnchorsAccountID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving Spatial Anchors Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if props := resp.AccountProperties; props != nil {
d.Set("account_domain", props.AccountDomain)
d.Set("account_id", props.AccountID)
}
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceSpatialAnchorsAccountDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MixedReality.SpatialAnchorsAccountClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.SpatialAnchorsAccountID(d.Id())
if err != nil {
return err
}
response, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(response) {
return fmt.Errorf("deleting Spatial Anchors Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}
return nil
}