forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps_account_data_source.go
104 lines (83 loc) · 2.73 KB
/
maps_account_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
package maps
import (
"fmt"
"time"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/maps/sdk/2021-02-01/accounts"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/maps/validate"
"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/timeouts"
)
func dataSourceMapsAccount() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceMapsAccountRead,
Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.AccountName(),
},
"resource_group_name": azure.SchemaResourceGroupName(),
"sku_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"tags": tags.Schema(),
"x_ms_client_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"primary_access_key": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},
"secondary_access_key": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
func dataSourceMapsAccountRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Maps.AccountsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id := accounts.NewAccountID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}
d.SetId(id.ID())
d.Set("name", id.AccountName)
d.Set("resource_group_name", id.ResourceGroupName)
if model := resp.Model; model != nil {
d.Set("sku_name", model.Sku.Name)
if props := model.Properties; props != nil {
d.Set("x_ms_client_id", props.UniqueId)
}
if err := tags.FlattenAndSet(d, flattenTags(model.Tags)); err != nil {
return err
}
}
keysResp, err := client.ListKeys(ctx, id)
if err != nil {
return fmt.Errorf("retrieving Access Keys for %s: %+v", id, err)
}
if model := keysResp.Model; model != nil {
d.Set("primary_access_key", model.PrimaryKey)
d.Set("secondary_access_key", model.SecondaryKey)
}
return nil
}