|
| 1 | +/* |
| 2 | +Provides a resource to create an exclusive CLB Logset. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_clb_logset" "foo" { |
| 8 | + name = "clb_logset" |
| 9 | + perioid = 7 |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +Import |
| 14 | +
|
| 15 | +CLB attachment can be imported using the id, e.g. |
| 16 | +
|
| 17 | +``` |
| 18 | +$ terraform import tencentcloud_clb_logset.foo 4eb9e3a8-9c42-4b32-9ddf-e215e9c92764 |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 27 | + cls "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls/v20201016" |
| 28 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 29 | +) |
| 30 | + |
| 31 | +func resourceTencentCloudClbLogSet() *schema.Resource { |
| 32 | + return &schema.Resource{ |
| 33 | + Create: resourceTencentCloudClbLogSetCreate, |
| 34 | + Read: resourceTencentCloudClbLogSetRead, |
| 35 | + Delete: resourceTencentCloudClbLogSetDelete, |
| 36 | + //Update: resourceTencentCloudClbLogSetUpdate, |
| 37 | + Importer: &schema.ResourceImporter{ |
| 38 | + State: schema.ImportStatePassthrough, |
| 39 | + }, |
| 40 | + Schema: map[string]*schema.Schema{ |
| 41 | + "period": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Optional: true, |
| 44 | + ForceNew: true, |
| 45 | + Description: "Logset retention period in days. Maximun value is `90`.", |
| 46 | + }, |
| 47 | + "name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "Logset name, which unique and fixed `clb_logset` among all CLS logsets.", |
| 51 | + }, |
| 52 | + "create_time": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "Logset creation time.", |
| 56 | + }, |
| 57 | + "topic_count": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "Number of log topics in logset.", |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceTencentCloudClbLogSetRead(d *schema.ResourceData, meta interface{}) error { |
| 67 | + defer logElapsed("resource.tencentcloud_clb_logset.read")() |
| 68 | + defer inconsistentCheck(d, meta)() |
| 69 | + |
| 70 | + logId := getLogId(contextNil) |
| 71 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 72 | + service := ClsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 73 | + |
| 74 | + id := d.Id() |
| 75 | + |
| 76 | + info, err := service.DescribeClsLogSetById(ctx, id) |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + if info == nil { |
| 83 | + d.SetId("") |
| 84 | + return fmt.Errorf("resource `Logset` %s does not exist", id) |
| 85 | + } |
| 86 | + |
| 87 | + _ = d.Set("name", info.LogsetName) |
| 88 | + |
| 89 | + // |
| 90 | + //if _, ok := d.GetOk("period"); !ok { |
| 91 | + // _ = d.Set("period", info) |
| 92 | + //} |
| 93 | + _ = d.Set("create_time", info.CreateTime) |
| 94 | + _ = d.Set("topic_count", info.TopicCount) |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func resourceTencentCloudClbLogSetCreate(d *schema.ResourceData, meta interface{}) error { |
| 100 | + defer logElapsed("resource.tencentcloud_clb_logset.create")() |
| 101 | + defer clbActionMu.Unlock() |
| 102 | + clbActionMu.Lock() |
| 103 | + |
| 104 | + logId := getLogId(contextNil) |
| 105 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 106 | + service := ClbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 107 | + |
| 108 | + var ( |
| 109 | + period = d.Get("period").(int) |
| 110 | + ) |
| 111 | + |
| 112 | + // We're not support specify name and health logs for now |
| 113 | + id, err := service.CreateClbLogSet(ctx, "clb_logset", "", period) |
| 114 | + |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + d.SetId(id) |
| 120 | + |
| 121 | + return resourceTencentCloudClbLogSetRead(d, meta) |
| 122 | +} |
| 123 | + |
| 124 | +// All fields are now Computed/ForceNew, means it does not support update |
| 125 | +func resourceTencentCloudClbLogSetUpdate(d *schema.ResourceData, meta interface{}) error { |
| 126 | + defer logElapsed("resource.tencentcloud_clb_logset.update")() |
| 127 | + logId := getLogId(contextNil) |
| 128 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 129 | + service := ClsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 130 | + request := cls.NewModifyLogsetRequest() |
| 131 | + |
| 132 | + request.LogsetId = helper.String(d.Id()) |
| 133 | + |
| 134 | + if d.HasChange("name") { |
| 135 | + request.LogsetName = helper.String(d.Get("name").(string)) |
| 136 | + } |
| 137 | + |
| 138 | + err := service.UpdateClsLogSet(ctx, request) |
| 139 | + |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + return resourceTencentCloudClbLogSetCreate(d, meta) |
| 145 | +} |
| 146 | + |
| 147 | +func resourceTencentCloudClbLogSetDelete(d *schema.ResourceData, meta interface{}) error { |
| 148 | + defer logElapsed("resource.tencentcloud_clb_logset.delete")() |
| 149 | + |
| 150 | + clbActionMu.Lock() |
| 151 | + defer clbActionMu.Unlock() |
| 152 | + |
| 153 | + logId := getLogId(contextNil) |
| 154 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 155 | + service := ClsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 156 | + id := d.Id() |
| 157 | + |
| 158 | + if err := service.DeleteClsLogSet(ctx, id); err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
0 commit comments