forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_huaweicloud_account.go
59 lines (47 loc) · 1.49 KB
/
data_source_huaweicloud_account.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
package huaweicloud
import (
"context"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/openstack/identity/v3/domains"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
// @API IAM GET /v3/auth/domains
func DataSourceAccount() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAccountRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAccountRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
identityClient, err := cfg.IdentityV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("Error creating IAM client: %s", err)
}
// ResourceBase: https://iam.{CLOUD}/v3/auth/
identityClient.ResourceBase += "auth/"
allPages, err := domains.List(identityClient, nil).AllPages()
if err != nil {
return diag.Errorf("failed to query account: %s", err)
}
accounts, err := domains.ExtractDomains(allPages)
if err != nil {
return diag.Errorf("failed to extract details of account: %s", err)
}
if len(accounts) == 0 {
return diag.Errorf("failed to query account: you are not authorized to perform the action")
}
result := accounts[0]
d.SetId(result.ID)
mErr := multierror.Append(nil,
d.Set("name", result.Name),
)
return diag.FromErr(mErr.ErrorOrNil())
}