|
| 1 | +package ucloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/customdiff" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 11 | + "github.com/ucloud/ucloud-sdk-go/ucloud" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceUCloudAntiDDoSAllowedDomain() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceUCloudAntiDDoSAllowedDomainCreate, |
| 17 | + Read: resourceUCloudAntiDDoSAllowedDomainRead, |
| 18 | + Update: resourceUCloudAntiDDoSAllowedDomainUpdate, |
| 19 | + Delete: resourceUCloudAntiDDoSAllowedDomainDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: schema.ImportStatePassthrough, |
| 22 | + }, |
| 23 | + |
| 24 | + CustomizeDiff: customdiff.All(), |
| 25 | + |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "domain": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + }, |
| 32 | + |
| 33 | + "comment": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + Computed: true, |
| 37 | + }, |
| 38 | + |
| 39 | + "instance_id": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Required: true, |
| 42 | + ForceNew: true, |
| 43 | + }, |
| 44 | + |
| 45 | + "status": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func resourceUCloudAntiDDoSAllowedDomainCreate(d *schema.ResourceData, meta interface{}) error { |
| 54 | + client := meta.(*UCloudClient) |
| 55 | + conn := client.uadsconn |
| 56 | + domain := d.Get("domain").(string) |
| 57 | + req := conn.NewAddNapAllowListDomainRequest() |
| 58 | + req.ResourceId = ucloud.String(d.Get("instance_id").(string)) |
| 59 | + req.Domain = []string{domain} |
| 60 | + |
| 61 | + resp, err := conn.AddNapAllowListDomain(req) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("error on creating ucloud_anti_ddos_domain, %s", err) |
| 64 | + } |
| 65 | + |
| 66 | + for _, data := range resp.Data { |
| 67 | + if data.Domain == domain && data.Code != 0 { |
| 68 | + return fmt.Errorf("fail to create domain %v to %v with RetCode %v", data.Domain, d.Id(), data.Code) |
| 69 | + } |
| 70 | + } |
| 71 | + instanceId := d.Get("instance_id").(string) |
| 72 | + d.SetId(fmt.Sprintf("%s/%s", instanceId, d.Get("domain").(string))) |
| 73 | + |
| 74 | + // after create lb, we need to wait it initialized |
| 75 | + stateConf := antiDDoSAllowedDomainWaitForState(client, instanceId, domain) |
| 76 | + |
| 77 | + _, err = stateConf.WaitForState() |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("error on waiting for ucloud_anti_ddos_instance %q creating, %s", d.Id(), err) |
| 80 | + } |
| 81 | + |
| 82 | + if comment, ok := d.GetOkExists("comment"); ok { |
| 83 | + remarkReq := conn.NewSetNapDomainEntryRemarkRequest() |
| 84 | + remarkReq.ResourceId = req.ResourceId |
| 85 | + remarkReq.Domain = ucloud.String(domain) |
| 86 | + remarkReq.Remark = ucloud.String(comment.(string)) |
| 87 | + _, err := conn.SetNapDomainEntryRemark(remarkReq) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("fail to set comment %v for domain %v of %v", comment, domain, *req.ResourceId) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return resourceUCloudAntiDDoSAllowedDomainRead(d, meta) |
| 94 | +} |
| 95 | + |
| 96 | +func resourceUCloudAntiDDoSAllowedDomainUpdate(d *schema.ResourceData, meta interface{}) error { |
| 97 | + client := meta.(*UCloudClient) |
| 98 | + conn := client.uadsconn |
| 99 | + d.Partial(true) |
| 100 | + instanceId := d.Get("instance_id").(string) |
| 101 | + domain := d.Get("domain").(string) |
| 102 | + |
| 103 | + if d.HasChange("comment") && !d.IsNewResource() { |
| 104 | + comment := d.Get("comment").(string) |
| 105 | + remarkReq := conn.NewSetNapDomainEntryRemarkRequest() |
| 106 | + remarkReq.ResourceId = ucloud.String(instanceId) |
| 107 | + remarkReq.Domain = ucloud.String(domain) |
| 108 | + remarkReq.Remark = ucloud.String(comment) |
| 109 | + _, err := conn.SetNapDomainEntryRemark(remarkReq) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("fail to set comment %v for domain %v of %v", comment, domain, instanceId) |
| 112 | + } |
| 113 | + d.SetPartial("comment") |
| 114 | + } |
| 115 | + |
| 116 | + d.Partial(false) |
| 117 | + |
| 118 | + return resourceUCloudAntiDDoSAllowedDomainRead(d, meta) |
| 119 | +} |
| 120 | + |
| 121 | +func resourceUCloudAntiDDoSAllowedDomainRead(d *schema.ResourceData, meta interface{}) error { |
| 122 | + client := meta.(*UCloudClient) |
| 123 | + id := d.Id() |
| 124 | + items := strings.SplitN(id, "/", 2) |
| 125 | + if len(items) != 2 { |
| 126 | + return fmt.Errorf("%v is an invalid ucloud_anti_ddos_allowed_domain id", id) |
| 127 | + } |
| 128 | + instanceId := items[0] |
| 129 | + domain := items[1] |
| 130 | + |
| 131 | + domainInfo, err := client.describeUADSAllowedDomain(instanceId, domain) |
| 132 | + |
| 133 | + if err != nil { |
| 134 | + if isNotFoundError(err) { |
| 135 | + d.SetId("") |
| 136 | + return nil |
| 137 | + } |
| 138 | + return fmt.Errorf("error on reading uads allowed domain %s of %s, %s", domain, instanceId, err) |
| 139 | + } |
| 140 | + d.Set("domain", domain) |
| 141 | + d.Set("instance_id", instanceId) |
| 142 | + d.Set("comment", domainInfo.Remark) |
| 143 | + d.Set("status", uadsAllowedDomainStatusCvt.convert(domainInfo.Status)) |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func resourceUCloudAntiDDoSAllowedDomainDelete(d *schema.ResourceData, meta interface{}) error { |
| 148 | + client := meta.(*UCloudClient) |
| 149 | + conn := client.uadsconn |
| 150 | + instanceId := d.Get("instance_id").(string) |
| 151 | + domain := d.Get("domain").(string) |
| 152 | + |
| 153 | + req := conn.NewDeleteNapAllowListDomainRequest() |
| 154 | + req.ResourceId = ucloud.String(instanceId) |
| 155 | + req.Domain = []string{domain} |
| 156 | + |
| 157 | + return resource.Retry(5*time.Minute, func() *resource.RetryError { |
| 158 | + if _, err := conn.DeleteNapAllowListDomain(req); err != nil { |
| 159 | + return resource.NonRetryableError(fmt.Errorf("error on deleting ucloud_anti_ddos_allowed_domain %s, %s", d.Id(), err)) |
| 160 | + } |
| 161 | + |
| 162 | + _, err := client.describeUADSAllowedDomain(instanceId, domain) |
| 163 | + if err != nil { |
| 164 | + if isNotFoundError(err) { |
| 165 | + return nil |
| 166 | + } |
| 167 | + return resource.NonRetryableError(fmt.Errorf("error on reading ucloud_anti_ddos_allowed_domain when deleting %s, %s", d.Id(), err)) |
| 168 | + } |
| 169 | + |
| 170 | + return resource.RetryableError(fmt.Errorf("the specified ucloud_anti_ddos_allowed_domain %s has not been deleted due to unknown error", d.Id())) |
| 171 | + }) |
| 172 | +} |
| 173 | + |
| 174 | +func antiDDoSAllowedDomainWaitForState(client *UCloudClient, id string, domain string) *resource.StateChangeConf { |
| 175 | + return &resource.StateChangeConf{ |
| 176 | + Pending: []string{statusPending}, |
| 177 | + Target: []string{statusInitialized}, |
| 178 | + Timeout: 3 * time.Minute, |
| 179 | + Delay: 2 * time.Second, |
| 180 | + MinTimeout: 1 * time.Second, |
| 181 | + Refresh: func() (interface{}, string, error) { |
| 182 | + domainInfo, err := client.describeUADSAllowedDomain(id, domain) |
| 183 | + if err != nil { |
| 184 | + if isNotFoundError(err) { |
| 185 | + return nil, statusPending, nil |
| 186 | + } |
| 187 | + return nil, "", err |
| 188 | + } |
| 189 | + |
| 190 | + if domainInfo.Status == uadsAllowedDomainStatusSuccess { |
| 191 | + return domainInfo, statusInitialized, nil |
| 192 | + } else if domainInfo.Status == uadsAllowedDomainStatusAdding { |
| 193 | + return domainInfo, statusPending, nil |
| 194 | + } else if domainInfo.Status == uadsAllowedDomainStatusFailure { |
| 195 | + return nil, "", fmt.Errorf("fail to add domain %v for %v", domain, id) |
| 196 | + } else { |
| 197 | + return nil, "", fmt.Errorf("status %v should not be got", domainInfo.Status) |
| 198 | + } |
| 199 | + }, |
| 200 | + } |
| 201 | +} |
0 commit comments