|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cam account_summary |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cam_account_summary" "account_summary" { |
| 8 | + } |
| 9 | +``` |
| 10 | +*/ |
| 11 | +package tencentcloud |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 18 | + cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116" |
| 19 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 20 | +) |
| 21 | + |
| 22 | +func dataSourceTencentCloudCamAccountSummary() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Read: dataSourceTencentCloudCamAccountSummaryRead, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "policies": { |
| 27 | + Computed: true, |
| 28 | + Type: schema.TypeInt, |
| 29 | + Description: "The number of policy.", |
| 30 | + }, |
| 31 | + |
| 32 | + "roles": { |
| 33 | + Computed: true, |
| 34 | + Type: schema.TypeInt, |
| 35 | + Description: "The number of role.", |
| 36 | + }, |
| 37 | + |
| 38 | + "user": { |
| 39 | + Computed: true, |
| 40 | + Type: schema.TypeInt, |
| 41 | + Description: "The number of Sub-user.", |
| 42 | + }, |
| 43 | + |
| 44 | + "group": { |
| 45 | + Computed: true, |
| 46 | + Type: schema.TypeInt, |
| 47 | + Description: "The number of Group.", |
| 48 | + }, |
| 49 | + |
| 50 | + "member": { |
| 51 | + Computed: true, |
| 52 | + Type: schema.TypeInt, |
| 53 | + Description: "The number of grouped users.", |
| 54 | + }, |
| 55 | + |
| 56 | + "identity_providers": { |
| 57 | + Computed: true, |
| 58 | + Type: schema.TypeInt, |
| 59 | + Description: "The number of identity provider.", |
| 60 | + }, |
| 61 | + |
| 62 | + "result_output_file": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Optional: true, |
| 65 | + Description: "Used to save results.", |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func dataSourceTencentCloudCamAccountSummaryRead(d *schema.ResourceData, meta interface{}) error { |
| 72 | + defer logElapsed("data_source.tencentcloud_cam_account_summary.read")() |
| 73 | + defer inconsistentCheck(d, meta)() |
| 74 | + |
| 75 | + logId := getLogId(contextNil) |
| 76 | + |
| 77 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 78 | + |
| 79 | + service := CamService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 80 | + AccountData := &cam.GetAccountSummaryResponseParams{} |
| 81 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 82 | + result, e := service.DescribeCamAccountSummaryByFilter(ctx) |
| 83 | + if e != nil { |
| 84 | + return retryError(e) |
| 85 | + } |
| 86 | + AccountData = result |
| 87 | + return nil |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + template := make(map[string]interface{}, 0) |
| 93 | + |
| 94 | + if AccountData.Policies != nil { |
| 95 | + _ = d.Set("policies", AccountData.Policies) |
| 96 | + template["policies"] = AccountData.Policies |
| 97 | + } |
| 98 | + |
| 99 | + if AccountData.Roles != nil { |
| 100 | + _ = d.Set("roles", AccountData.Roles) |
| 101 | + template["roles"] = AccountData.Roles |
| 102 | + } |
| 103 | + |
| 104 | + if AccountData.User != nil { |
| 105 | + _ = d.Set("user", AccountData.User) |
| 106 | + template["user"] = AccountData.User |
| 107 | + } |
| 108 | + |
| 109 | + if AccountData.Group != nil { |
| 110 | + _ = d.Set("group", AccountData.Group) |
| 111 | + template["group"] = AccountData.Group |
| 112 | + } |
| 113 | + |
| 114 | + if AccountData.Member != nil { |
| 115 | + _ = d.Set("member", AccountData.Member) |
| 116 | + template["member"] = AccountData.Member |
| 117 | + } |
| 118 | + |
| 119 | + if AccountData.IdentityProviders != nil { |
| 120 | + _ = d.Set("identity_providers", AccountData.IdentityProviders) |
| 121 | + template["identity_providers"] = AccountData.IdentityProviders |
| 122 | + } |
| 123 | + d.SetId(helper.BuildToken()) |
| 124 | + output, ok := d.GetOk("result_output_file") |
| 125 | + if ok && output.(string) != "" { |
| 126 | + if e := writeToFile(output.(string), template); e != nil { |
| 127 | + return e |
| 128 | + } |
| 129 | + } |
| 130 | + return nil |
| 131 | +} |
0 commit comments