Skip to content

Commit

Permalink
Add import failover and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Venelin Ganarev committed Aug 20, 2024
1 parent 6514add commit ce8885b
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ resource "cloudns_dns_zone" "some-zone" {
}
```

```sh
terraform import cloudns_dns_zone.some-zone "example.com"
```

### Import Failover

Failover can be imported using:

```sh
terraform import ADDR "domain"
```

Example zone and its import command:

```hcl
resource "cloudns_dns_zone" "some-zone" {
# example.com
domain = "example.com"
type = "master"
}
```

```sh
terraform import cloudns_dns_zone.some-zone "example.com"
```
78 changes: 78 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,81 @@ provider "cloudns" {
- `password` (String, Sensitive) This is the password associated with your auth-id or sub-auth-id. It is read from the environment variable `CLOUDNS_PASSWORD` if not passed explicitly.
- `rate_limit` (Number) Underlying rate limit (in API calls per second) to observe while interacting with ClouDNS. Defaults to 5 requests per second.
- `sub_auth_id` (Number, Sensitive) When using api sub-users, this is the `sub-auth-id`. It is read from the environment variable `CLOUDNS_SUB_AUTH_ID` if not passed explicitly. Mutually exclusive with `auth_id`.


### Import Records

Records can be imported using:

```sh
terraform import ADDR "zone/id"
```

Example record and its import command:

```hcl
resource "cloudns_dns_record" "some-record" {
# ID: 123456789
# something.cloudns.net 600 in A 1.2.3.4
name = ""
zone = "something.cloudns.net"
type = "A"
value = "1.2.3.4"
ttl = "600"
}
```

```sh
terraform import cloudns_dns_record.some-record "something.cloudns.net/123456789"
```

### Import Zones

Zones can be imported using:

```sh
terraform import ADDR "recordId"
```

The recordId is the ID of the record on which the failover is activated

Example failover and its import command:

```hcl
resource "cloudns_dns_failover" "testzone-bg-http" {
domain = cloudns_dns_zone.sub-testzone-bg.domain
recordid = cloudns_dns_record.sub-testzone-bg-a["something"].id
checktype = "9"
port = "90"
downeventhandler = "0"
upeventhandler = "0"
mainip = cloudns_dns_record.sub-testzone-bg-a["something"].value
depends_on = [ cloudns_dns_zone.sub-testzone-bg ]
}
```

```sh
terraform import cloudns_dns_failover.testzone-bg-http "recordID"
```

### Import Failover

Failover can be imported using:

```sh
terraform import ADDR "domain"
```

Example zone and its import command:

```hcl
resource "cloudns_dns_zone" "some-zone" {
# example.com
domain = "example.com"
type = "master"
}
```

```sh
terraform import cloudns_dns_zone.some-zone "example.com"
```
27 changes: 26 additions & 1 deletion internal/cloudns/resource_failover_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func resourceDnsFailover() *schema.Resource {
DeleteContext: resourceDnsFailoverDelete,

Importer: &schema.ResourceImporter{
StateContext: resourceDnsZoneImport,
StateContext: resourceDnsFailoverImport,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -235,6 +235,31 @@ func resourceDnsFailoverDelete(ctx context.Context, d *schema.ResourceData, meta
return nil
}

func resourceDnsFailoverImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(ClientConfig)
RecordId := d.Id()

config.rateLimiter.Take()
failoverRead, err := cloudns.Failover{RecordId: RecordId}.Read(&config.apiAccess)
if err != nil {
return nil, err
}

if failoverRead.RecordId == "" {
return nil, fmt.Errorf("Failover not found: %#v", failoverRead)
}

err = updateFailoverState(d, &failoverRead)
if err != nil {
return nil, err
}
d.SetId(RecordId)

tflog.Debug(ctx, fmt.Sprintf("IMPORT Failover %s", RecordId))

return []*schema.ResourceData{d}, nil
}

func toApiFailover(d *schema.ResourceData) cloudns.Failover {
domain := d.Get("domain").(string)
recordId := d.Get("recordid").(string)
Expand Down

0 comments on commit ce8885b

Please sign in to comment.