|
| 1 | +package tco |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331" |
| 12 | + |
| 13 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 14 | +) |
| 15 | + |
| 16 | +func DataSourceTencentCloudOrganizationServices() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Read: dataSourceTencentCloudOrganizationServicesRead, |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "search_key": { |
| 21 | + Optional: true, |
| 22 | + Type: schema.TypeString, |
| 23 | + Description: "Keyword for search by name.", |
| 24 | + }, |
| 25 | + // computed |
| 26 | + "items": { |
| 27 | + Computed: true, |
| 28 | + Type: schema.TypeList, |
| 29 | + Description: "Organization service list.", |
| 30 | + Elem: &schema.Resource{ |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "service_id": { |
| 33 | + Type: schema.TypeInt, |
| 34 | + Computed: true, |
| 35 | + Description: "Organization service ID. Note: This field may return null, indicating that no valid values can be obtained.", |
| 36 | + }, |
| 37 | + "product_name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + Description: "Organization service product name. Note: This field may return null, indicating that no valid values can be obtained.", |
| 41 | + }, |
| 42 | + "is_assign": { |
| 43 | + Type: schema.TypeInt, |
| 44 | + Computed: true, |
| 45 | + Description: "Whether to support delegation. Valid values: 1 (yes), 2 (no). Note: This field may return null, indicating that no valid values can be obtained.", |
| 46 | + }, |
| 47 | + "description": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "Organization service description. Note: This field may return null, indicating that no valid values can be obtained.", |
| 51 | + }, |
| 52 | + "member_num": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "Number of the current delegated admins. Note: This field may return null, indicating that no valid values can be obtained.", |
| 56 | + }, |
| 57 | + "document": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "Help documentation. Note: This field may return null, indicating that no valid values can be obtained.", |
| 61 | + }, |
| 62 | + "console_url": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "Console path of the organization service product. Note: This field may return null, indicating that no valid values can be obtained.", |
| 66 | + }, |
| 67 | + "is_usage_status": { |
| 68 | + Type: schema.TypeInt, |
| 69 | + Computed: true, |
| 70 | + Description: "Whether to access the usage status. Valid values: 1 (yes), 2 (no). Note: This field may return null, indicating that no valid values can be obtained.", |
| 71 | + }, |
| 72 | + "can_assign_count": { |
| 73 | + Type: schema.TypeInt, |
| 74 | + Computed: true, |
| 75 | + Description: "Limit for the number of delegated admins. Note: This field may return null, indicating that no valid values can be obtained.", |
| 76 | + }, |
| 77 | + "product": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "Organization service product identifier. Note: This field may return null, indicating that no valid values can be obtained.", |
| 81 | + }, |
| 82 | + "service_grant": { |
| 83 | + Type: schema.TypeInt, |
| 84 | + Computed: true, |
| 85 | + Description: "Whether to support organization service authorization. Valid values: 1 (yes), 2 (no). Note: This field may return null, indicating that no valid values can be obtained.", |
| 86 | + }, |
| 87 | + "grant_status": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "Enabling status of organization service authorization. This field is valid when ServiceGrant is 1. Valid values: Enabled, Disabled. Note: This field may return null, indicating that no valid values can be obtained.", |
| 91 | + }, |
| 92 | + "is_set_management_scope": { |
| 93 | + Type: schema.TypeInt, |
| 94 | + Computed: true, |
| 95 | + Description: "Whether to support setting the delegated management scope. Valid values: 1 (yes), 2 (no).\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + "result_output_file": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Optional: true, |
| 103 | + Description: "Used to save results.", |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func dataSourceTencentCloudOrganizationServicesRead(d *schema.ResourceData, meta interface{}) error { |
| 110 | + defer tccommon.LogElapsed("data_source.tencentcloud_organization_services.read")() |
| 111 | + defer tccommon.InconsistentCheck(d, meta)() |
| 112 | + |
| 113 | + var ( |
| 114 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 115 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 116 | + service = OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 117 | + items []*organization.OrganizationServiceAssign |
| 118 | + ) |
| 119 | + |
| 120 | + paramMap := make(map[string]interface{}) |
| 121 | + |
| 122 | + if v, ok := d.GetOk("search_key"); ok { |
| 123 | + paramMap["SearchKey"] = helper.String(v.(string)) |
| 124 | + } |
| 125 | + |
| 126 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 127 | + result, e := service.DescribeOrganizationServicesByFilter(ctx, paramMap) |
| 128 | + if e != nil { |
| 129 | + return tccommon.RetryError(e) |
| 130 | + } |
| 131 | + |
| 132 | + items = result |
| 133 | + return nil |
| 134 | + }) |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + ids := make([]string, 0, len(items)) |
| 141 | + tmpList := make([]map[string]interface{}, 0, len(items)) |
| 142 | + |
| 143 | + if items != nil { |
| 144 | + for _, item := range items { |
| 145 | + orgServiceAssignMap := map[string]interface{}{} |
| 146 | + |
| 147 | + if item.ServiceId != nil { |
| 148 | + orgServiceAssignMap["service_id"] = item.ServiceId |
| 149 | + serviceIdStr := strconv.FormatUint(*item.ServiceId, 10) |
| 150 | + ids = append(ids, serviceIdStr) |
| 151 | + } |
| 152 | + |
| 153 | + if item.ProductName != nil { |
| 154 | + orgServiceAssignMap["product_name"] = item.ProductName |
| 155 | + } |
| 156 | + |
| 157 | + if item.IsAssign != nil { |
| 158 | + orgServiceAssignMap["is_assign"] = item.IsAssign |
| 159 | + } |
| 160 | + |
| 161 | + if item.Description != nil { |
| 162 | + orgServiceAssignMap["description"] = item.Description |
| 163 | + } |
| 164 | + |
| 165 | + if item.MemberNum != nil { |
| 166 | + orgServiceAssignMap["member_num"] = item.MemberNum |
| 167 | + } |
| 168 | + |
| 169 | + if item.ConsoleUrl != nil { |
| 170 | + orgServiceAssignMap["console_url"] = item.ConsoleUrl |
| 171 | + } |
| 172 | + |
| 173 | + if item.IsUsageStatus != nil { |
| 174 | + orgServiceAssignMap["is_usage_status"] = item.IsUsageStatus |
| 175 | + } |
| 176 | + |
| 177 | + if item.CanAssignCount != nil { |
| 178 | + orgServiceAssignMap["can_assign_count"] = item.CanAssignCount |
| 179 | + } |
| 180 | + |
| 181 | + if item.Product != nil { |
| 182 | + orgServiceAssignMap["product"] = item.Product |
| 183 | + } |
| 184 | + |
| 185 | + if item.ServiceGrant != nil { |
| 186 | + orgServiceAssignMap["service_grant"] = item.ServiceGrant |
| 187 | + } |
| 188 | + |
| 189 | + if item.GrantStatus != nil { |
| 190 | + orgServiceAssignMap["grant_status"] = item.GrantStatus |
| 191 | + } |
| 192 | + |
| 193 | + if item.IsSetManagementScope != nil { |
| 194 | + orgServiceAssignMap["is_set_management_scope"] = item.IsSetManagementScope |
| 195 | + } |
| 196 | + |
| 197 | + tmpList = append(tmpList, orgServiceAssignMap) |
| 198 | + } |
| 199 | + |
| 200 | + _ = d.Set("items", tmpList) |
| 201 | + } |
| 202 | + |
| 203 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 204 | + output, ok := d.GetOk("result_output_file") |
| 205 | + if ok && output.(string) != "" { |
| 206 | + if e := tccommon.WriteToFile(output.(string), tmpList); e != nil { |
| 207 | + return e |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + return nil |
| 212 | +} |
0 commit comments