Skip to content

Commit

Permalink
- Add civo_dns_domain_name and civo_dns_domain_record
Browse files Browse the repository at this point in the history
- Fix some error in the resource_network.go and resource_volume.go

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Mar 12, 2020
1 parent 7f7d75d commit 73ebfd3
Show file tree
Hide file tree
Showing 7 changed files with 589 additions and 16 deletions.
8 changes: 5 additions & 3 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ func Provider() terraform.ResourceProvider {
},
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
"civo_network": resourceNetwork(),
"civo_volume": resourceVolume(),
"civo_instance": resourceInstance(),
"civo_network": resourceNetwork(),
"civo_volume": resourceVolume(),
"civo_dns_domain_name": resourceDnsDomainName(),
"civo_dns_domain_record": resourceDnsDomainRecord(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
107 changes: 107 additions & 0 deletions civo/resource_dns_domain_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package civo

import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)

func resourceDnsDomainName() *schema.Resource {
fmt.Print()
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "A fully qualified domain name",
ValidateFunc: validateName,
},
// Computed resource
"account_id": {
Type: schema.TypeString,
Computed: true,
},
},
Create: resourceDnsDomainNameCreate,
Read: resourceDnsDomainNameRead,
Update: resourceDnsDomainNameUpdate,
Delete: resourceDnsDomainNameDelete,
//Exists: resourceExistsItem,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

func resourceDnsDomainNameCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

dnsDomain, err := apiClient.CreateDNSDomain(d.Get("name").(string))
if err != nil {
fmt.Errorf("failed to create a new domains: %s", err)
return err
}

d.SetId(dnsDomain.ID)

return resourceDnsDomainNameRead(d, m)
}

func resourceDnsDomainNameRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

resp, err := apiClient.GetDNSDomain(d.Get("name").(string))
if err != nil {
if resp != nil {
d.SetId("")
return nil
}

return fmt.Errorf("error retrieving domain: %s", err)
}

d.Set("name", resp.Name)
d.Set("account_id", resp.AccountID)

return nil
}

func resourceDnsDomainNameUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

resp, err := apiClient.FindDNSDomain(d.Id())
if err != nil {
log.Printf("[WARN] Civo domain (%s) not found", d.Id())
d.SetId("")
return nil
}

if d.HasChange("name") {
name := d.Get("name").(string)
_, err := apiClient.UpdateDNSDomain(resp, name)
if err != nil {
log.Printf("[WARN] An error occurred while renamed the domain (%s)", d.Id())
}

}

return resourceDnsDomainNameRead(d, m)
}

func resourceDnsDomainNameDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

resp, err := apiClient.FindDNSDomain(d.Id())
if err != nil {
log.Printf("[WARN] Civo domain (%s) not found", d.Id())
d.SetId("")
return nil
}

_, err = apiClient.DeleteDNSDomain(resp)
if err != nil {
log.Printf("[INFO] Civo domain (%s) was delete", d.Id())
}
return nil
}
215 changes: 215 additions & 0 deletions civo/resource_dns_domain_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package civo

import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"log"
)

const (
// DNSRecordTypeA represents an A record
DNSRecordTypeA = "a"

// DNSRecordTypeCName represents an CNAME record
DNSRecordTypeCName = "cname"

// DNSRecordTypeMX represents an MX record
DNSRecordTypeMX = "mx"

// DNSRecordTypeTXT represents an TXT record
DNSRecordTypeTXT = "txt"
)

func resourceDnsDomainRecord() *schema.Resource {
fmt.Print()
return &schema.Resource{
Schema: map[string]*schema.Schema{
"domain_id": {
Type: schema.TypeString,
Required: true,
Description: "Id from domain name",
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "The choice of RR type from a, cname, mx or txt",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The portion before the domain name (e.g. www) or an @ for the apex/root domain (you cannot use an A record with an amex/root domain)",
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "The IP address (A or MX), hostname (CNAME or MX) or text value (TXT) to serve for this record",
ValidateFunc: validation.NoZeroValues,
},
"priority": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.NoZeroValues,
Description: "Useful for MX records only, the priority mail should be attempted it (defaults to 10)",
},
"ttl": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.NoZeroValues,
Description: "How long caching DNS servers should cache this record for, in seconds (the minimum is 600 and the default if unspecified is 600)",
},
// Computed resource
"account_id": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
Create: resourceDnsDomainRecordCreate,
Read: resourceDnsDomainRecordRead,
Update: resourceDnsDomainRecordUpdate,
Delete: resourceDnsDomainRecordDelete,
//Exists: resourceExistsItem,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

func resourceDnsDomainRecordCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

config := &civogo.DNSRecordConfig{
Name: d.Get("name").(string),
Value: d.Get("value").(string),
TTL: d.Get("ttl").(int),
}

if attr, ok := d.GetOk("priority"); ok {
if d.Get("type").(string) != "mx" {
return fmt.Errorf("[WARN] warning priority value is only allow in the MX records")
}
config.Priority = attr.(int)
}

if d.Get("type").(string) == "a" {
config.Type = DNSRecordTypeA
}

if d.Get("type").(string) == "cname" {
config.Type = DNSRecordTypeCName
}

if d.Get("type").(string) == "mx" {
config.Type = DNSRecordTypeMX
}

if d.Get("type").(string) == "txt" {
config.Type = DNSRecordTypeTXT
}

dnsDomainRecord, err := apiClient.CreateDNSRecord(d.Get("domain_id").(string), config)
if err != nil {
fmt.Errorf("failed to create a new record: %s", err)
return err
}

d.SetId(dnsDomainRecord.ID)

return resourceDnsDomainRecordRead(d, m)
}

func resourceDnsDomainRecordRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

resp, err := apiClient.GetDNSRecord(d.Get("domain_id").(string), d.Id())
if err != nil {
log.Printf("[WARN] civo domain record (%s) not found", d.Id())
d.SetId("")
return nil
}

d.Set("name", resp.Name)
d.Set("account_id", resp.AccountID)
d.Set("domain_id", resp.DNSDomainID)
d.Set("name", resp.Name)
d.Set("value", resp.Value)
d.Set("type", resp.Type)
d.Set("priority", resp.Priority)
d.Set("ttl", resp.TTL)
d.Set("created_at", resp.CreatedAt.String())
d.Set("updated_at", resp.UpdatedAt.String())

return nil
}

func resourceDnsDomainRecordUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

resp, err := apiClient.GetDNSRecord(d.Get("domain_id").(string), d.Id())
if err != nil {
log.Printf("[WARN] civo domain record (%s) not found", d.Id())
d.SetId("")
return nil
}

config := &civogo.DNSRecordConfig{}

if d.HasChange("name") || d.HasChange("value") || d.HasChange("priority") || d.HasChange("ttl") || d.HasChange("type") {
config.Name = d.Get("name").(string)
config.Value = d.Get("value").(string)
config.Priority = d.Get("priority").(int)
config.TTL = d.Get("ttl").(int)

if d.Get("type").(string) == "a" {
config.Type = DNSRecordTypeA
}

if d.Get("type").(string) == "cname" {
config.Type = DNSRecordTypeCName
}

if d.Get("type").(string) == "mx" {
config.Type = DNSRecordTypeMX
}

if d.Get("type").(string) == "txt" {
config.Type = DNSRecordTypeTXT
}
}

_, err = apiClient.UpdateDNSRecord(resp, config)
if err != nil {
log.Printf("[WARN] an error occurred while renamed the domain record (%s)", d.Id())
}

return resourceDnsDomainRecordRead(d, m)
}

func resourceDnsDomainRecordDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

resp, err := apiClient.GetDNSRecord(d.Get("domain_id").(string), d.Id())
if err != nil {
log.Printf("[WARN] civo domain record (%s) not found", d.Id())
d.SetId("")
return nil
}

_, err = apiClient.DeleteDNSRecord(resp)
if err != nil {
log.Printf("[WARN] civo domain record (%s) not found", d.Id())
d.SetId("")
return nil
}

return nil
}
9 changes: 2 additions & 7 deletions civo/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ func resourceNetwork() *schema.Resource {

func resourceNetworkCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

config := &civogo.NetworkConfig{Label: d.Get("label").(string)}

network, err := apiClient.NewNetwork(config)
network, err := apiClient.NewNetwork(d.Get("label").(string))
if err != nil {
fmt.Errorf("failed to create a new config: %s", err)
return err
Expand Down Expand Up @@ -91,9 +88,7 @@ func resourceNetworkUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

if d.HasChange("label") {
config := &civogo.NetworkConfig{Label: d.Get("label").(string)}

_, err := apiClient.RenameNetwork(config, d.Id())
_, err := apiClient.RenameNetwork(d.Get("label").(string), d.Id())
if err != nil {
log.Printf("[WARN] An error occurred while rename the network (%s)", d.Id())
}
Expand Down
4 changes: 2 additions & 2 deletions civo/resource_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func resourceVolume() *schema.Resource {
func resourceVolumeCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

config := &civogo.VolumeConfig{Name: d.Get("name").(string), SizeGB: d.Get("size_gb").(int), Bootable: d.Get("bootable").(bool)}
config := &civogo.VolumeConfig{Name: d.Get("name").(string), SizeGigabytes: d.Get("size_gb").(int), Bootable: d.Get("bootable").(bool)}

volume, err := apiClient.NewVolume(config)
if err != nil {
Expand Down Expand Up @@ -94,7 +94,7 @@ func resourceVolumeRead(d *schema.ResourceData, m interface{}) error {
}

d.Set("name", CurrentVolume.Name)
d.Set("size_gb", CurrentVolume.SizeGB)
d.Set("size_gb", CurrentVolume.SizeGigabytes)
d.Set("bootable", CurrentVolume.Bootable)
d.Set("instance_id", CurrentVolume.InstanceID)
d.Set("mount_point", CurrentVolume.MountPoint)
Expand Down
Loading

0 comments on commit 73ebfd3

Please sign in to comment.