|
| 1 | +/* |
| 2 | +Provides a resource to create a apiGateway plugin_attachment |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_api_gateway_plugin_attachment" "plugin_attachment" { |
| 8 | + plugin_id = "plugin-ny74siyz" |
| 9 | + service_id = "service-n1mgl0sq" |
| 10 | + environment_name = "test" |
| 11 | + api_id = "api-6tfrdysk" |
| 12 | +} |
| 13 | +``` |
| 14 | +
|
| 15 | +Import |
| 16 | +
|
| 17 | +apiGateway plugin_attachment can be imported using the id, e.g. |
| 18 | +
|
| 19 | +``` |
| 20 | +terraform import tencentcloud_api_gateway_plugin_attachment.plugin_attachment pluginId#serviceId#environmentName#apiId |
| 21 | +``` |
| 22 | +*/ |
| 23 | +package tencentcloud |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "log" |
| 29 | + "strings" |
| 30 | + |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 32 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 33 | + apiGateway "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway/v20180808" |
| 34 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 35 | +) |
| 36 | + |
| 37 | +func resourceTencentCloudApiGatewayPluginAttachment() *schema.Resource { |
| 38 | + return &schema.Resource{ |
| 39 | + Create: resourceTencentCloudApiGatewayPluginAttachmentCreate, |
| 40 | + Read: resourceTencentCloudApiGatewayPluginAttachmentRead, |
| 41 | + Delete: resourceTencentCloudApiGatewayPluginAttachmentDelete, |
| 42 | + Importer: &schema.ResourceImporter{ |
| 43 | + State: schema.ImportStatePassthrough, |
| 44 | + }, |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "plugin_id": { |
| 47 | + Required: true, |
| 48 | + ForceNew: true, |
| 49 | + Type: schema.TypeString, |
| 50 | + Description: "Id of Plugin.", |
| 51 | + }, |
| 52 | + |
| 53 | + "service_id": { |
| 54 | + Required: true, |
| 55 | + ForceNew: true, |
| 56 | + Type: schema.TypeString, |
| 57 | + Description: "Id of Service.", |
| 58 | + }, |
| 59 | + |
| 60 | + "environment_name": { |
| 61 | + Required: true, |
| 62 | + ForceNew: true, |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "Name of Environment.", |
| 65 | + }, |
| 66 | + |
| 67 | + "api_id": { |
| 68 | + Required: true, |
| 69 | + ForceNew: true, |
| 70 | + Type: schema.TypeString, |
| 71 | + Description: "Id of API.", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func resourceTencentCloudApiGatewayPluginAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 78 | + defer logElapsed("resource.tencentcloud_api_gateway_plugin_attachment.create")() |
| 79 | + defer inconsistentCheck(d, meta)() |
| 80 | + |
| 81 | + logId := getLogId(contextNil) |
| 82 | + |
| 83 | + var ( |
| 84 | + request = apiGateway.NewAttachPluginRequest() |
| 85 | + pluginId string |
| 86 | + serviceId string |
| 87 | + environmentName string |
| 88 | + apiId string |
| 89 | + ) |
| 90 | + if v, ok := d.GetOk("plugin_id"); ok { |
| 91 | + pluginId = v.(string) |
| 92 | + request.PluginId = helper.String(v.(string)) |
| 93 | + } |
| 94 | + |
| 95 | + if v, ok := d.GetOk("service_id"); ok { |
| 96 | + serviceId = v.(string) |
| 97 | + request.ServiceId = helper.String(v.(string)) |
| 98 | + } |
| 99 | + |
| 100 | + if v, ok := d.GetOk("environment_name"); ok { |
| 101 | + environmentName = v.(string) |
| 102 | + request.EnvironmentName = helper.String(v.(string)) |
| 103 | + } |
| 104 | + |
| 105 | + if v, ok := d.GetOk("api_id"); ok { |
| 106 | + apiId = v.(string) |
| 107 | + request.ApiIds = []*string{helper.String(v.(string))} |
| 108 | + } |
| 109 | + |
| 110 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 111 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseAPIGatewayClient().AttachPlugin(request) |
| 112 | + if e != nil { |
| 113 | + return retryError(e) |
| 114 | + } else { |
| 115 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 116 | + } |
| 117 | + return nil |
| 118 | + }) |
| 119 | + if err != nil { |
| 120 | + log.Printf("[CRITAL]%s create apiGateway pluginAttachment failed, reason:%+v", logId, err) |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + d.SetId(pluginId + FILED_SP + serviceId + FILED_SP + environmentName + FILED_SP + apiId) |
| 125 | + |
| 126 | + return resourceTencentCloudApiGatewayPluginAttachmentRead(d, meta) |
| 127 | +} |
| 128 | + |
| 129 | +func resourceTencentCloudApiGatewayPluginAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 130 | + defer logElapsed("resource.tencentcloud_api_gateway_plugin_attachment.read")() |
| 131 | + defer inconsistentCheck(d, meta)() |
| 132 | + |
| 133 | + logId := getLogId(contextNil) |
| 134 | + |
| 135 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 136 | + |
| 137 | + service := APIGatewayService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 138 | + |
| 139 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 140 | + if len(idSplit) != 4 { |
| 141 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 142 | + } |
| 143 | + pluginId := idSplit[0] |
| 144 | + serviceId := idSplit[1] |
| 145 | + environmentName := idSplit[2] |
| 146 | + apiId := idSplit[3] |
| 147 | + |
| 148 | + pluginAttachment, err := service.DescribeApiGatewayPluginAttachmentById(ctx, pluginId, serviceId, environmentName, apiId) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + if pluginAttachment == nil { |
| 154 | + d.SetId("") |
| 155 | + return fmt.Errorf("resource `ApiGatewayPluginAttachment` %s does not exist", d.Id()) |
| 156 | + } |
| 157 | + |
| 158 | + _ = d.Set("plugin_id", pluginId) |
| 159 | + |
| 160 | + if pluginAttachment.ServiceId != nil { |
| 161 | + _ = d.Set("service_id", pluginAttachment.ServiceId) |
| 162 | + } |
| 163 | + |
| 164 | + if pluginAttachment.Environment != nil { |
| 165 | + _ = d.Set("environment_name", pluginAttachment.Environment) |
| 166 | + } |
| 167 | + |
| 168 | + if pluginAttachment.ApiId != nil { |
| 169 | + _ = d.Set("api_id", pluginAttachment.ApiId) |
| 170 | + } |
| 171 | + |
| 172 | + return nil |
| 173 | +} |
| 174 | + |
| 175 | +func resourceTencentCloudApiGatewayPluginAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 176 | + defer logElapsed("resource.tencentcloud_api_gateway_plugin_attachment.delete")() |
| 177 | + defer inconsistentCheck(d, meta)() |
| 178 | + |
| 179 | + logId := getLogId(contextNil) |
| 180 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 181 | + |
| 182 | + service := APIGatewayService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 183 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 184 | + if len(idSplit) != 4 { |
| 185 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 186 | + } |
| 187 | + pluginId := idSplit[0] |
| 188 | + serviceId := idSplit[1] |
| 189 | + environmentName := idSplit[2] |
| 190 | + apiId := idSplit[3] |
| 191 | + |
| 192 | + if err := service.DeleteApiGatewayPluginAttachmentById(ctx, pluginId, serviceId, environmentName, apiId); err != nil { |
| 193 | + return err |
| 194 | + } |
| 195 | + |
| 196 | + return nil |
| 197 | +} |
0 commit comments