|
| 1 | +package tencentcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + gaap "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap/v20180529" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceTencentCloudGaapProxyGroup() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceTencentCloudGaapProxyGroupCreate, |
| 17 | + Read: resourceTencentCloudGaapProxyGroupRead, |
| 18 | + Update: resourceTencentCloudGaapProxyGroupUpdate, |
| 19 | + Delete: resourceTencentCloudGaapProxyGroupDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: schema.ImportStatePassthrough, |
| 22 | + }, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "project_id": { |
| 25 | + Required: true, |
| 26 | + Type: schema.TypeInt, |
| 27 | + Description: "ID of the project to which the proxy group belongs.", |
| 28 | + }, |
| 29 | + |
| 30 | + "group_name": { |
| 31 | + Required: true, |
| 32 | + Type: schema.TypeString, |
| 33 | + Description: "Channel group alias.", |
| 34 | + }, |
| 35 | + |
| 36 | + "real_server_region": { |
| 37 | + Required: true, |
| 38 | + Type: schema.TypeString, |
| 39 | + Description: "real server region, refer to the interface DescribeDestRegions to return the RegionId in the parameter RegionDetail.", |
| 40 | + }, |
| 41 | + |
| 42 | + "ip_address_version": { |
| 43 | + Optional: true, |
| 44 | + Type: schema.TypeString, |
| 45 | + Description: "IP version, can be taken as IPv4 or IPv6 with a default value of IPv4.", |
| 46 | + }, |
| 47 | + |
| 48 | + "package_type": { |
| 49 | + Optional: true, |
| 50 | + Type: schema.TypeString, |
| 51 | + Description: "Package type of channel group. Available values: Thunder and Accelerator. Default is Thunder.", |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func resourceTencentCloudGaapProxyGroupCreate(d *schema.ResourceData, meta interface{}) error { |
| 58 | + defer logElapsed("resource.tencentcloud_gaap_proxy_group.create")() |
| 59 | + defer inconsistentCheck(d, meta)() |
| 60 | + |
| 61 | + logId := getLogId(contextNil) |
| 62 | + |
| 63 | + var ( |
| 64 | + request = gaap.NewCreateProxyGroupRequest() |
| 65 | + response = gaap.NewCreateProxyGroupResponse() |
| 66 | + groupId string |
| 67 | + ) |
| 68 | + if v, ok := d.GetOkExists("project_id"); ok { |
| 69 | + request.ProjectId = helper.IntUint64(v.(int)) |
| 70 | + } |
| 71 | + |
| 72 | + if v, ok := d.GetOk("group_name"); ok { |
| 73 | + request.GroupName = helper.String(v.(string)) |
| 74 | + } |
| 75 | + |
| 76 | + if v, ok := d.GetOk("real_server_region"); ok { |
| 77 | + request.RealServerRegion = helper.String(v.(string)) |
| 78 | + } |
| 79 | + |
| 80 | + if v, ok := d.GetOk("ip_address_version"); ok { |
| 81 | + request.IPAddressVersion = helper.String(v.(string)) |
| 82 | + } |
| 83 | + |
| 84 | + if v, ok := d.GetOk("package_type"); ok { |
| 85 | + request.PackageType = helper.String(v.(string)) |
| 86 | + } |
| 87 | + |
| 88 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 89 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseGaapClient().CreateProxyGroup(request) |
| 90 | + if e != nil { |
| 91 | + return retryError(e) |
| 92 | + } else { |
| 93 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 94 | + } |
| 95 | + response = result |
| 96 | + return nil |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + log.Printf("[CRITAL]%s create gaap proxyGroup failed, reason:%+v", logId, err) |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + groupId = *response.Response.GroupId |
| 104 | + d.SetId(groupId) |
| 105 | + |
| 106 | + return resourceTencentCloudGaapProxyGroupRead(d, meta) |
| 107 | +} |
| 108 | + |
| 109 | +func resourceTencentCloudGaapProxyGroupRead(d *schema.ResourceData, meta interface{}) error { |
| 110 | + defer logElapsed("resource.tencentcloud_gaap_proxy_group.read")() |
| 111 | + defer inconsistentCheck(d, meta)() |
| 112 | + |
| 113 | + logId := getLogId(contextNil) |
| 114 | + |
| 115 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 116 | + |
| 117 | + service := GaapService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 118 | + |
| 119 | + groupId := d.Id() |
| 120 | + |
| 121 | + proxyGroup, err := service.DescribeGaapProxyGroupById(ctx, groupId) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + if proxyGroup == nil { |
| 127 | + d.SetId("") |
| 128 | + log.Printf("[WARN]%s resource `GaapProxyGroup` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 129 | + return nil |
| 130 | + } |
| 131 | + |
| 132 | + if proxyGroup.ProjectId != nil { |
| 133 | + _ = d.Set("project_id", proxyGroup.ProjectId) |
| 134 | + } |
| 135 | + |
| 136 | + if proxyGroup.GroupName != nil { |
| 137 | + _ = d.Set("group_name", proxyGroup.GroupName) |
| 138 | + } |
| 139 | + |
| 140 | + if proxyGroup.RealServerRegionInfo != nil && proxyGroup.RealServerRegionInfo.RegionId != nil { |
| 141 | + _ = d.Set("real_server_region", proxyGroup.RealServerRegionInfo.RegionId) |
| 142 | + } |
| 143 | + |
| 144 | + if proxyGroup.IPAddressVersion != nil { |
| 145 | + _ = d.Set("ip_address_version", proxyGroup.IPAddressVersion) |
| 146 | + } |
| 147 | + |
| 148 | + if proxyGroup.PackageType != nil { |
| 149 | + _ = d.Set("package_type", proxyGroup.PackageType) |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func resourceTencentCloudGaapProxyGroupUpdate(d *schema.ResourceData, meta interface{}) error { |
| 156 | + defer logElapsed("resource.tencentcloud_gaap_proxy_group.update")() |
| 157 | + defer inconsistentCheck(d, meta)() |
| 158 | + |
| 159 | + logId := getLogId(contextNil) |
| 160 | + |
| 161 | + request := gaap.NewModifyProxyGroupAttributeRequest() |
| 162 | + |
| 163 | + groupId := d.Id() |
| 164 | + |
| 165 | + request.GroupId = &groupId |
| 166 | + |
| 167 | + immutableArgs := []string{"real_server_region", "ip_address_version", "package_type"} |
| 168 | + |
| 169 | + for _, v := range immutableArgs { |
| 170 | + if d.HasChange(v) { |
| 171 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + isChanged := false |
| 176 | + if d.HasChange("project_id") { |
| 177 | + if v, ok := d.GetOkExists("project_id"); ok { |
| 178 | + request.ProjectId = helper.IntUint64(v.(int)) |
| 179 | + isChanged = true |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if d.HasChange("group_name") { |
| 184 | + if v, ok := d.GetOk("group_name"); ok { |
| 185 | + request.GroupName = helper.String(v.(string)) |
| 186 | + isChanged = true |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if isChanged { |
| 191 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 192 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseGaapClient().ModifyProxyGroupAttribute(request) |
| 193 | + if e != nil { |
| 194 | + return retryError(e) |
| 195 | + } else { |
| 196 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 197 | + } |
| 198 | + return nil |
| 199 | + }) |
| 200 | + if err != nil { |
| 201 | + log.Printf("[CRITAL]%s update gaap proxyGroup failed, reason:%+v", logId, err) |
| 202 | + return err |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return resourceTencentCloudGaapProxyGroupRead(d, meta) |
| 207 | +} |
| 208 | + |
| 209 | +func resourceTencentCloudGaapProxyGroupDelete(d *schema.ResourceData, meta interface{}) error { |
| 210 | + defer logElapsed("resource.tencentcloud_gaap_proxy_group.delete")() |
| 211 | + defer inconsistentCheck(d, meta)() |
| 212 | + |
| 213 | + logId := getLogId(contextNil) |
| 214 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 215 | + |
| 216 | + service := GaapService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 217 | + groupId := d.Id() |
| 218 | + |
| 219 | + if err := service.DeleteGaapProxyGroupById(ctx, groupId); err != nil { |
| 220 | + return err |
| 221 | + } |
| 222 | + |
| 223 | + return nil |
| 224 | +} |
0 commit comments