mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-02 12:17:39 -06:00
proviers/digitalocean: add domains
This commit is contained in:
parent
1a94a239e4
commit
d2a01f2209
101
builtin/providers/digitalocean/resource_digitalocean_domain.go
Normal file
101
builtin/providers/digitalocean/resource_digitalocean_domain.go
Normal file
@ -0,0 +1,101 @@
|
||||
package digitalocean
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/config"
|
||||
"github.com/hashicorp/terraform/helper/diff"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/pearkes/digitalocean"
|
||||
)
|
||||
|
||||
func resource_digitalocean_domain_create(
|
||||
s *terraform.ResourceState,
|
||||
d *terraform.ResourceDiff,
|
||||
meta interface{}) (*terraform.ResourceState, error) {
|
||||
p := meta.(*ResourceProvider)
|
||||
client := p.client
|
||||
// Merge the diff into the state so that we have all the attributes
|
||||
// properly.
|
||||
rs := s.MergeDiff(d)
|
||||
|
||||
// Build up our creation options
|
||||
opts := digitalocean.CreateDomain{
|
||||
Name: rs.Attributes["name"],
|
||||
IPAddress: rs.Attributes["ip_address"],
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Domain create configuration: %#v", opts)
|
||||
|
||||
name, err := client.CreateDomain(&opts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error creating Domain: %s", err)
|
||||
}
|
||||
|
||||
rs.ID = name
|
||||
log.Printf("[INFO] Domain Name: %s", name)
|
||||
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
func resource_digitalocean_domain_destroy(
|
||||
s *terraform.ResourceState,
|
||||
meta interface{}) error {
|
||||
p := meta.(*ResourceProvider)
|
||||
client := p.client
|
||||
|
||||
log.Printf("[INFO] Deleting Domain: %s", s.ID)
|
||||
|
||||
err := client.DestroyDomain(s.ID)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error deleting Domain: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resource_digitalocean_domain_refresh(
|
||||
s *terraform.ResourceState,
|
||||
meta interface{}) (*terraform.ResourceState, error) {
|
||||
p := meta.(*ResourceProvider)
|
||||
client := p.client
|
||||
|
||||
domain, err := client.RetrieveDomain(s.ID)
|
||||
|
||||
if err != nil {
|
||||
return s, fmt.Errorf("Error retrieving domain: %s", err)
|
||||
}
|
||||
|
||||
s.Attributes["name"] = domain.Name
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func resource_digitalocean_domain_diff(
|
||||
s *terraform.ResourceState,
|
||||
c *terraform.ResourceConfig,
|
||||
meta interface{}) (*terraform.ResourceDiff, error) {
|
||||
|
||||
b := &diff.ResourceBuilder{
|
||||
Attrs: map[string]diff.AttrType{
|
||||
"name": diff.AttrTypeCreate,
|
||||
"ip_address": diff.AttrTypeCreate,
|
||||
},
|
||||
|
||||
ComputedAttrs: []string{},
|
||||
}
|
||||
|
||||
return b.Diff(s, c)
|
||||
}
|
||||
|
||||
func resource_digitalocean_domain_validation() *config.Validator {
|
||||
return &config.Validator{
|
||||
Required: []string{
|
||||
"name",
|
||||
"ip_address",
|
||||
},
|
||||
Optional: []string{},
|
||||
}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
package digitalocean
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/pearkes/digitalocean"
|
||||
)
|
||||
|
||||
func TestAccDigitalOceanDomain_Basic(t *testing.T) {
|
||||
var domain digitalocean.Domain
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckDigitalOceanDomainDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccCheckDigitalOceanDomainConfig_basic,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckDigitalOceanDomainExists("digitalocean_domain.foobar", &domain),
|
||||
testAccCheckDigitalOceanDomainAttributes(&domain),
|
||||
resource.TestCheckResourceAttr(
|
||||
"digitalocean_domain.foobar", "name", "foobar-test-terraform.com"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"digitalocean_domain.foobar", "ip_address", "192.168.0.10"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckDigitalOceanDomainDestroy(s *terraform.State) error {
|
||||
client := testAccProvider.client
|
||||
|
||||
for _, rs := range s.Resources {
|
||||
if rs.Type != "digitalocean_domain" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Try to find the domain
|
||||
_, err := client.RetrieveDomain(rs.ID)
|
||||
|
||||
if err == nil {
|
||||
fmt.Errorf("Domain still exists")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccCheckDigitalOceanDomainAttributes(domain *digitalocean.Domain) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
|
||||
if domain.Name != "foobar-test-terraform.com" {
|
||||
return fmt.Errorf("Bad name: %s", domain.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckDigitalOceanDomainExists(n string, domain *digitalocean.Domain) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
rs, ok := s.Resources[n]
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found: %s", n)
|
||||
}
|
||||
|
||||
if rs.ID == "" {
|
||||
return fmt.Errorf("No Record ID is set")
|
||||
}
|
||||
|
||||
client := testAccProvider.client
|
||||
|
||||
foundDomain, err := client.RetrieveDomain(rs.ID)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if foundDomain.Name != rs.ID {
|
||||
return fmt.Errorf("Record not found")
|
||||
}
|
||||
|
||||
*domain = foundDomain
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
const testAccCheckDigitalOceanDomainConfig_basic = `
|
||||
resource "digitalocean_domain" "foobar" {
|
||||
name = "foobar-test-terraform.com"
|
||||
ip_address = "192.168.0.10"
|
||||
}`
|
@ -11,6 +11,14 @@ var resourceMap *resource.Map
|
||||
func init() {
|
||||
resourceMap = &resource.Map{
|
||||
Mapping: map[string]resource.Resource{
|
||||
"digitalocean_domain": resource.Resource{
|
||||
ConfigValidator: resource_digitalocean_domain_validation(),
|
||||
Create: resource_digitalocean_domain_create,
|
||||
Destroy: resource_digitalocean_domain_destroy,
|
||||
Diff: resource_digitalocean_domain_diff,
|
||||
Refresh: resource_digitalocean_domain_refresh,
|
||||
},
|
||||
|
||||
"digitalocean_droplet": resource.Resource{
|
||||
ConfigValidator: resource_digitalocean_droplet_validation(),
|
||||
Create: resource_digitalocean_droplet_create,
|
||||
|
Loading…
Reference in New Issue
Block a user