|
| 1 | +/* |
| 2 | +Provides a resource to create a apiGateway plugin |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_api_gateway_plugin" "plugin" { |
| 8 | + plugin_name = "terraform-plugin-test" |
| 9 | + plugin_type = "IPControl" |
| 10 | + plugin_data = jsonencode({ |
| 11 | + "type" : "white_list", |
| 12 | + "blocks" : "1.1.1.1\n2.2.2.2", |
| 13 | + }) |
| 14 | + description = "terraform test" |
| 15 | +} |
| 16 | +``` |
| 17 | +
|
| 18 | +Import |
| 19 | +
|
| 20 | +apiGateway plugin can be imported using the id, e.g. |
| 21 | +
|
| 22 | +``` |
| 23 | +terraform import tencentcloud_api_gateway_plugin.plugin plugin_id |
| 24 | +``` |
| 25 | +*/ |
| 26 | +package tencentcloud |
| 27 | + |
| 28 | +import ( |
| 29 | + "context" |
| 30 | + "fmt" |
| 31 | + "log" |
| 32 | + |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 34 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 35 | + apiGateway "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway/v20180808" |
| 36 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 37 | +) |
| 38 | + |
| 39 | +func resourceTencentCloudApiGatewayPlugin() *schema.Resource { |
| 40 | + return &schema.Resource{ |
| 41 | + Create: resourceTencentCloudApiGatewayPluginCreate, |
| 42 | + Read: resourceTencentCloudApiGatewayPluginRead, |
| 43 | + Update: resourceTencentCloudApiGatewayPluginUpdate, |
| 44 | + Delete: resourceTencentCloudApiGatewayPluginDelete, |
| 45 | + Importer: &schema.ResourceImporter{ |
| 46 | + State: schema.ImportStatePassthrough, |
| 47 | + }, |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "plugin_name": { |
| 50 | + Required: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Name of the user define plugin. It must start with a letter and end with letter or number, the rest can contain letters, numbers and dashes(-). The length range is from 2 to 50.", |
| 53 | + }, |
| 54 | + |
| 55 | + "plugin_type": { |
| 56 | + Required: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "Type of plugin. Now support IPControl, TrafficControl, Cors, CustomReq, CustomAuth, Routing, TrafficControlByParameter, CircuitBreaker, ProxyCache.", |
| 59 | + }, |
| 60 | + |
| 61 | + "plugin_data": { |
| 62 | + Required: true, |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "Statement to define plugin.", |
| 65 | + }, |
| 66 | + |
| 67 | + "description": { |
| 68 | + Optional: true, |
| 69 | + Type: schema.TypeString, |
| 70 | + Description: "Description of plugin.", |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func resourceTencentCloudApiGatewayPluginCreate(d *schema.ResourceData, meta interface{}) error { |
| 77 | + defer logElapsed("resource.tencentcloud_api_gateway_plugin.create")() |
| 78 | + defer inconsistentCheck(d, meta)() |
| 79 | + |
| 80 | + logId := getLogId(contextNil) |
| 81 | + |
| 82 | + var ( |
| 83 | + request = apiGateway.NewCreatePluginRequest() |
| 84 | + response = apiGateway.NewCreatePluginResponse() |
| 85 | + pluginId string |
| 86 | + ) |
| 87 | + if v, ok := d.GetOk("plugin_name"); ok { |
| 88 | + request.PluginName = helper.String(v.(string)) |
| 89 | + } |
| 90 | + |
| 91 | + if v, ok := d.GetOk("plugin_type"); ok { |
| 92 | + request.PluginType = helper.String(v.(string)) |
| 93 | + } |
| 94 | + |
| 95 | + if v, ok := d.GetOk("plugin_data"); ok { |
| 96 | + request.PluginData = helper.String(v.(string)) |
| 97 | + } |
| 98 | + |
| 99 | + if v, ok := d.GetOk("description"); ok { |
| 100 | + request.Description = helper.String(v.(string)) |
| 101 | + } |
| 102 | + |
| 103 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 104 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseAPIGatewayClient().CreatePlugin(request) |
| 105 | + if e != nil { |
| 106 | + return retryError(e) |
| 107 | + } else { |
| 108 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 109 | + } |
| 110 | + response = result |
| 111 | + return nil |
| 112 | + }) |
| 113 | + if err != nil { |
| 114 | + log.Printf("[CRITAL]%s create apiGateway plugin failed, reason:%+v", logId, err) |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + pluginId = *response.Response.Result.PluginId |
| 119 | + d.SetId(pluginId) |
| 120 | + |
| 121 | + return resourceTencentCloudApiGatewayPluginRead(d, meta) |
| 122 | +} |
| 123 | + |
| 124 | +func resourceTencentCloudApiGatewayPluginRead(d *schema.ResourceData, meta interface{}) error { |
| 125 | + defer logElapsed("resource.tencentcloud_api_gateway_plugin.read")() |
| 126 | + defer inconsistentCheck(d, meta)() |
| 127 | + |
| 128 | + logId := getLogId(contextNil) |
| 129 | + |
| 130 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 131 | + |
| 132 | + service := APIGatewayService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 133 | + |
| 134 | + pluginId := d.Id() |
| 135 | + |
| 136 | + plugin, err := service.DescribeApiGatewayPluginById(ctx, pluginId) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + if plugin == nil { |
| 142 | + d.SetId("") |
| 143 | + return fmt.Errorf("resource `ApiGatewayPlugin` %s does not exist", d.Id()) |
| 144 | + } |
| 145 | + |
| 146 | + if plugin.PluginName != nil { |
| 147 | + _ = d.Set("plugin_name", plugin.PluginName) |
| 148 | + } |
| 149 | + |
| 150 | + if plugin.PluginType != nil { |
| 151 | + _ = d.Set("plugin_type", plugin.PluginType) |
| 152 | + } |
| 153 | + |
| 154 | + if plugin.PluginData != nil { |
| 155 | + _ = d.Set("plugin_data", plugin.PluginData) |
| 156 | + } |
| 157 | + |
| 158 | + if plugin.Description != nil { |
| 159 | + _ = d.Set("description", plugin.Description) |
| 160 | + } |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func resourceTencentCloudApiGatewayPluginUpdate(d *schema.ResourceData, meta interface{}) error { |
| 166 | + defer logElapsed("resource.tencentcloud_api_gateway_plugin.update")() |
| 167 | + defer inconsistentCheck(d, meta)() |
| 168 | + |
| 169 | + logId := getLogId(contextNil) |
| 170 | + |
| 171 | + unsupportedUpdateFields := []string{ |
| 172 | + "plugin_type", |
| 173 | + } |
| 174 | + for _, field := range unsupportedUpdateFields { |
| 175 | + if d.HasChange(field) { |
| 176 | + return fmt.Errorf("tencentcloud_api_gateway_plugin update on %s is not support yet", field) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + request := apiGateway.NewModifyPluginRequest() |
| 181 | + |
| 182 | + pluginId := d.Id() |
| 183 | + |
| 184 | + request.PluginId = &pluginId |
| 185 | + |
| 186 | + if d.HasChange("plugin_name") { |
| 187 | + if v, ok := d.GetOk("plugin_name"); ok { |
| 188 | + request.PluginName = helper.String(v.(string)) |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + if d.HasChange("plugin_data") { |
| 193 | + if v, ok := d.GetOk("plugin_data"); ok { |
| 194 | + request.PluginData = helper.String(v.(string)) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if d.HasChange("description") { |
| 199 | + if v, ok := d.GetOk("description"); ok { |
| 200 | + request.Description = helper.String(v.(string)) |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 205 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseAPIGatewayClient().ModifyPlugin(request) |
| 206 | + if e != nil { |
| 207 | + return retryError(e) |
| 208 | + } else { |
| 209 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 210 | + } |
| 211 | + return nil |
| 212 | + }) |
| 213 | + if err != nil { |
| 214 | + log.Printf("[CRITAL]%s update apiGateway plugin failed, reason:%+v", logId, err) |
| 215 | + return err |
| 216 | + } |
| 217 | + |
| 218 | + return resourceTencentCloudApiGatewayPluginRead(d, meta) |
| 219 | +} |
| 220 | + |
| 221 | +func resourceTencentCloudApiGatewayPluginDelete(d *schema.ResourceData, meta interface{}) error { |
| 222 | + defer logElapsed("resource.tencentcloud_api_gateway_plugin.delete")() |
| 223 | + defer inconsistentCheck(d, meta)() |
| 224 | + |
| 225 | + logId := getLogId(contextNil) |
| 226 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 227 | + |
| 228 | + service := APIGatewayService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 229 | + pluginId := d.Id() |
| 230 | + |
| 231 | + if err := service.DeleteApiGatewayPluginById(ctx, pluginId); err != nil { |
| 232 | + return err |
| 233 | + } |
| 234 | + |
| 235 | + return nil |
| 236 | +} |
0 commit comments