Added the ability to create a domain without specifying the inital A records IP address

This commit is contained in:
Tilen Faganel 2018-08-26 21:56:45 +01:00
parent fe9492c23d
commit 993d73f797
No known key found for this signature in database
GPG Key ID: 3DDA5ABF228F8E7A
3 changed files with 45 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/digitalocean/godo" "github.com/digitalocean/godo"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
) )
func resourceDigitalOceanDomain() *schema.Resource { func resourceDigitalOceanDomain() *schema.Resource {
@ -20,15 +21,17 @@ func resourceDigitalOceanDomain() *schema.Resource {
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": { "name": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
ValidateFunc: validation.NoZeroValues,
}, },
"ip_address": { "ip_address": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
ForceNew: true, ForceNew: true,
ValidateFunc: validation.NoZeroValues,
}, },
}, },
} }
@ -40,8 +43,11 @@ func resourceDigitalOceanDomainCreate(d *schema.ResourceData, meta interface{})
// Build up our creation options // Build up our creation options
opts := &godo.DomainCreateRequest{ opts := &godo.DomainCreateRequest{
Name: d.Get("name").(string), Name: d.Get("name").(string),
IPAddress: d.Get("ip_address").(string), }
if v, ok := d.GetOk("ip_address"); ok {
opts.IPAddress = v.(string)
} }
log.Printf("[DEBUG] Domain create configuration: %#v", opts) log.Printf("[DEBUG] Domain create configuration: %#v", opts)

View File

@ -35,6 +35,30 @@ func TestAccDigitalOceanDomain_Basic(t *testing.T) {
}) })
} }
func TestAccDigitalOceanDomain_WithoutIp(t *testing.T) {
var domain godo.Domain
domainName := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDigitalOceanDomainDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDomainConfig_withoutIp, domainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanDomainExists("digitalocean_domain.foobar", &domain),
testAccCheckDigitalOceanDomainAttributes(&domain, domainName),
resource.TestCheckResourceAttr(
"digitalocean_domain.foobar", "name", domainName),
resource.TestCheckNoResourceAttr(
"digitalocean_domain.foobar", "ip_address"),
),
},
},
})
}
func testAccCheckDigitalOceanDomainDestroy(s *terraform.State) error { func testAccCheckDigitalOceanDomainDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*godo.Client) client := testAccProvider.Meta().(*godo.Client)
@ -100,3 +124,8 @@ resource "digitalocean_domain" "foobar" {
name = "%s" name = "%s"
ip_address = "192.168.0.10" ip_address = "192.168.0.10"
}` }`
const testAccCheckDigitalOceanDomainConfig_withoutIp = `
resource "digitalocean_domain" "foobar" {
name = "%s"
}`

View File

@ -25,9 +25,8 @@ resource "digitalocean_domain" "default" {
The following arguments are supported: The following arguments are supported:
* `name` - (Required) The name of the domain * `name` - (Required) The name of the domain
* `ip_address` - (Required) The IP address of the domain. This IP * `ip_address` - (Optional) The IP address of the domain. If specified, this IP
is used to created an initial A record for the domain. It is required is used to created an initial A record for the domain.
upstream by the DigitalOcean API.
## Attributes Reference ## Attributes Reference
@ -35,8 +34,6 @@ The following attributes are exported:
* `id` - The name of the domain * `id` - The name of the domain
## Import ## Import
Domains can be imported using the `domain name`, e.g. Domains can be imported using the `domain name`, e.g.