|
| 1 | +package kms |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudKmsServiceStatus() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudKmsServiceStatusRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "service_enabled": { |
| 19 | + Type: schema.TypeBool, |
| 20 | + Computed: true, |
| 21 | + Description: "Whether the KMS service has been activated. true: activated.", |
| 22 | + }, |
| 23 | + |
| 24 | + "invalid_type": { |
| 25 | + Type: schema.TypeInt, |
| 26 | + Computed: true, |
| 27 | + Description: "Service unavailability type. 0: not purchased; 1: normal; 2: suspended due to arrears; 3: resource released.", |
| 28 | + }, |
| 29 | + |
| 30 | + "user_level": { |
| 31 | + Type: schema.TypeInt, |
| 32 | + Computed: true, |
| 33 | + Description: "0: Basic Edition, 1: Ultimate Edition.", |
| 34 | + }, |
| 35 | + |
| 36 | + "pro_expire_time": { |
| 37 | + Type: schema.TypeInt, |
| 38 | + Computed: true, |
| 39 | + Description: "Expiration time of the KMS Ultimate edition. It's represented in a Unix Epoch timestamp.\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 40 | + }, |
| 41 | + |
| 42 | + "pro_renew_flag": { |
| 43 | + Type: schema.TypeInt, |
| 44 | + Computed: true, |
| 45 | + Description: "Whether to automatically renew Ultimate Edition. 0: no, 1: yes\nNote: this field may return null, indicating that no valid values can be obtained.", |
| 46 | + }, |
| 47 | + |
| 48 | + "pro_resource_id": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + Description: "Unique ID of the Ultimate Edition purchase record. If the Ultimate Edition is not activated, the returned value will be null.\nNote: this field may return null, indicating that no valid values can be obtained.", |
| 52 | + }, |
| 53 | + |
| 54 | + "exclusive_vsm_enabled": { |
| 55 | + Type: schema.TypeBool, |
| 56 | + Computed: true, |
| 57 | + Description: "Whether to activate Managed KMS\nNote: This field may return `null`, indicating that no valid value can be obtained.", |
| 58 | + }, |
| 59 | + |
| 60 | + "exclusive_hsm_enabled": { |
| 61 | + Type: schema.TypeBool, |
| 62 | + Computed: true, |
| 63 | + Description: "Whether to activate Exclusive KMS\nNote: This field may return `null`, indicating that no valid value can be obtained.", |
| 64 | + }, |
| 65 | + |
| 66 | + "subscription_info": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "KMS subscription information.\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 70 | + }, |
| 71 | + |
| 72 | + "cmk_user_count": { |
| 73 | + Type: schema.TypeInt, |
| 74 | + Computed: true, |
| 75 | + Description: "Return the number of KMS user key usage.", |
| 76 | + }, |
| 77 | + |
| 78 | + "cmk_limit": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Computed: true, |
| 81 | + Description: "Return KMS user key specification quantity.", |
| 82 | + }, |
| 83 | + |
| 84 | + "exclusive_hsm_list": { |
| 85 | + Type: schema.TypeList, |
| 86 | + Computed: true, |
| 87 | + Description: "Return to Exclusive Cluster Group.", |
| 88 | + Elem: &schema.Resource{ |
| 89 | + Schema: map[string]*schema.Schema{ |
| 90 | + "hsm_cluster_id": { |
| 91 | + Type: schema.TypeInt, |
| 92 | + Required: true, |
| 93 | + Description: "Exclusive cluster ID.", |
| 94 | + }, |
| 95 | + "hsm_cluster_name": { |
| 96 | + Type: schema.TypeInt, |
| 97 | + Required: true, |
| 98 | + Description: "Exclusive cluster name.", |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + |
| 104 | + "result_output_file": { |
| 105 | + Type: schema.TypeString, |
| 106 | + Optional: true, |
| 107 | + Description: "Used to save results.", |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func dataSourceTencentCloudKmsServiceStatusRead(d *schema.ResourceData, meta interface{}) error { |
| 114 | + defer tccommon.LogElapsed("data_source.tencentcloud_kms_service_status.read")() |
| 115 | + defer tccommon.InconsistentCheck(d, meta)() |
| 116 | + |
| 117 | + var ( |
| 118 | + logId = tccommon.GetLogId(nil) |
| 119 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 120 | + service = KmsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 121 | + ) |
| 122 | + |
| 123 | + paramMap := make(map[string]interface{}) |
| 124 | + var respData *kms.GetServiceStatusResponseParams |
| 125 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 126 | + result, e := service.DescribeKmsServiceStatusByFilter(ctx, paramMap) |
| 127 | + if e != nil { |
| 128 | + return tccommon.RetryError(e) |
| 129 | + } |
| 130 | + |
| 131 | + respData = result |
| 132 | + return nil |
| 133 | + }) |
| 134 | + |
| 135 | + if reqErr != nil { |
| 136 | + return reqErr |
| 137 | + } |
| 138 | + |
| 139 | + if respData.ServiceEnabled != nil { |
| 140 | + _ = d.Set("service_enabled", respData.ServiceEnabled) |
| 141 | + } |
| 142 | + |
| 143 | + if respData.InvalidType != nil { |
| 144 | + _ = d.Set("invalid_type", respData.InvalidType) |
| 145 | + } |
| 146 | + |
| 147 | + if respData.UserLevel != nil { |
| 148 | + _ = d.Set("user_level", respData.UserLevel) |
| 149 | + } |
| 150 | + |
| 151 | + if respData.ProExpireTime != nil { |
| 152 | + _ = d.Set("pro_expire_time", respData.ProExpireTime) |
| 153 | + } |
| 154 | + |
| 155 | + if respData.ProRenewFlag != nil { |
| 156 | + _ = d.Set("pro_renew_flag", respData.ProRenewFlag) |
| 157 | + } |
| 158 | + |
| 159 | + if respData.ProResourceId != nil { |
| 160 | + _ = d.Set("pro_resource_id", respData.ProResourceId) |
| 161 | + } |
| 162 | + |
| 163 | + if respData.ExclusiveVSMEnabled != nil { |
| 164 | + _ = d.Set("exclusive_vsm_enabled", respData.ExclusiveVSMEnabled) |
| 165 | + } |
| 166 | + |
| 167 | + if respData.ExclusiveHSMEnabled != nil { |
| 168 | + _ = d.Set("exclusive_hsm_enabled", respData.ExclusiveHSMEnabled) |
| 169 | + } |
| 170 | + |
| 171 | + if respData.SubscriptionInfo != nil { |
| 172 | + _ = d.Set("subscription_info", respData.SubscriptionInfo) |
| 173 | + } |
| 174 | + |
| 175 | + if respData.CmkUserCount != nil { |
| 176 | + _ = d.Set("cmk_user_count", respData.CmkUserCount) |
| 177 | + } |
| 178 | + |
| 179 | + if respData.CmkLimit != nil { |
| 180 | + _ = d.Set("cmk_limit", respData.CmkLimit) |
| 181 | + } |
| 182 | + |
| 183 | + if respData.ExclusiveHSMList != nil { |
| 184 | + tmpList := make([]map[string]interface{}, 0, len(respData.ExclusiveHSMList)) |
| 185 | + for _, item := range respData.ExclusiveHSMList { |
| 186 | + dMap := make(map[string]interface{}) |
| 187 | + if item.HsmClusterId != nil { |
| 188 | + dMap["hsm_cluster_id"] = item.HsmClusterId |
| 189 | + } |
| 190 | + |
| 191 | + if item.HsmClusterName != nil { |
| 192 | + dMap["hsm_cluster_name"] = item.HsmClusterName |
| 193 | + } |
| 194 | + |
| 195 | + tmpList = append(tmpList, dMap) |
| 196 | + } |
| 197 | + |
| 198 | + _ = d.Set("exclusive_hsm_list", tmpList) |
| 199 | + } |
| 200 | + |
| 201 | + d.SetId(helper.BuildToken()) |
| 202 | + output, ok := d.GetOk("result_output_file") |
| 203 | + if ok && output.(string) != "" { |
| 204 | + if e := tccommon.WriteToFile(output.(string), d); e != nil { |
| 205 | + return e |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + return nil |
| 210 | +} |
0 commit comments