|
|
|
|
@ -13,6 +13,12 @@ import (
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var directoryCreationFuncs = map[string]func(*directoryservice.DirectoryService, *schema.ResourceData) (string, error){
|
|
|
|
|
"SimpleAD": createSimpleDirectoryService,
|
|
|
|
|
"MicrosoftAD": createActiveDirectoryService,
|
|
|
|
|
"ADConnector": createDirectoryConnector,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resourceAwsDirectoryServiceDirectory() *schema.Resource {
|
|
|
|
|
return &schema.Resource{
|
|
|
|
|
Create: resourceAwsDirectoryServiceDirectoryCreate,
|
|
|
|
|
@ -33,7 +39,7 @@ func resourceAwsDirectoryServiceDirectory() *schema.Resource {
|
|
|
|
|
},
|
|
|
|
|
"size": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Required: true,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
"alias": &schema.Schema{
|
|
|
|
|
@ -55,7 +61,8 @@ func resourceAwsDirectoryServiceDirectory() *schema.Resource {
|
|
|
|
|
},
|
|
|
|
|
"vpc_settings": &schema.Schema{
|
|
|
|
|
Type: schema.TypeList,
|
|
|
|
|
Required: true,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
|
"subnet_ids": &schema.Schema{
|
|
|
|
|
@ -73,6 +80,39 @@ func resourceAwsDirectoryServiceDirectory() *schema.Resource {
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"connect_settings": &schema.Schema{
|
|
|
|
|
Type: schema.TypeList,
|
|
|
|
|
Optional: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
|
"customer_username": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Required: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
"customer_dns_ips": &schema.Schema{
|
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
|
Required: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
|
Set: schema.HashString,
|
|
|
|
|
},
|
|
|
|
|
"subnet_ids": &schema.Schema{
|
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
|
Required: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
|
Set: schema.HashString,
|
|
|
|
|
},
|
|
|
|
|
"vpc_id": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Required: true,
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"enable_sso": &schema.Schema{
|
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
|
Optional: true,
|
|
|
|
|
@ -90,55 +130,192 @@ func resourceAwsDirectoryServiceDirectory() *schema.Resource {
|
|
|
|
|
},
|
|
|
|
|
"type": &schema.Schema{
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
Computed: true,
|
|
|
|
|
Optional: true,
|
|
|
|
|
Default: "SimpleAD",
|
|
|
|
|
ForceNew: true,
|
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
|
validTypes := []string{"SimpleAD", "MicrosoftAD"}
|
|
|
|
|
value := v.(string)
|
|
|
|
|
for validType, _ := range directoryCreationFuncs {
|
|
|
|
|
if validType == value {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
es = append(es, fmt.Errorf("%q must be one of %q", k, validTypes))
|
|
|
|
|
return
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resourceAwsDirectoryServiceDirectoryCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
|
dsconn := meta.(*AWSClient).dsconn
|
|
|
|
|
func buildVpcSettings(d *schema.ResourceData) (vpcSettings *directoryservice.DirectoryVpcSettings, err error) {
|
|
|
|
|
if v, ok := d.GetOk("vpc_settings"); !ok {
|
|
|
|
|
return nil, fmt.Errorf("vpc_settings is required for type = SimpleAD or MicrosoftAD")
|
|
|
|
|
} else {
|
|
|
|
|
settings := v.([]interface{})
|
|
|
|
|
|
|
|
|
|
input := directoryservice.CreateDirectoryInput{
|
|
|
|
|
Name: aws.String(d.Get("name").(string)),
|
|
|
|
|
Password: aws.String(d.Get("password").(string)),
|
|
|
|
|
Size: aws.String(d.Get("size").(string)),
|
|
|
|
|
}
|
|
|
|
|
if len(settings) > 1 {
|
|
|
|
|
return nil, fmt.Errorf("Only a single vpc_settings block is expected")
|
|
|
|
|
} else if len(settings) == 1 {
|
|
|
|
|
s := settings[0].(map[string]interface{})
|
|
|
|
|
var subnetIds []*string
|
|
|
|
|
for _, id := range s["subnet_ids"].(*schema.Set).List() {
|
|
|
|
|
subnetIds = append(subnetIds, aws.String(id.(string)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
|
input.Description = aws.String(v.(string))
|
|
|
|
|
}
|
|
|
|
|
if v, ok := d.GetOk("short_name"); ok {
|
|
|
|
|
input.ShortName = aws.String(v.(string))
|
|
|
|
|
vpcSettings = &directoryservice.DirectoryVpcSettings{
|
|
|
|
|
SubnetIds: subnetIds,
|
|
|
|
|
VpcId: aws.String(s["vpc_id"].(string)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("vpc_settings"); ok {
|
|
|
|
|
return vpcSettings, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func buildConnectSettings(d *schema.ResourceData) (connectSettings *directoryservice.DirectoryConnectSettings, err error) {
|
|
|
|
|
if v, ok := d.GetOk("connect_settings"); !ok {
|
|
|
|
|
return nil, fmt.Errorf("connect_settings is required for type = ADConnector")
|
|
|
|
|
} else {
|
|
|
|
|
settings := v.([]interface{})
|
|
|
|
|
|
|
|
|
|
if len(settings) > 1 {
|
|
|
|
|
return fmt.Errorf("Only a single vpc_settings block is expected")
|
|
|
|
|
return nil, fmt.Errorf("Only a single connect_settings block is expected")
|
|
|
|
|
} else if len(settings) == 1 {
|
|
|
|
|
s := settings[0].(map[string]interface{})
|
|
|
|
|
|
|
|
|
|
var subnetIds []*string
|
|
|
|
|
for _, id := range s["subnet_ids"].(*schema.Set).List() {
|
|
|
|
|
subnetIds = append(subnetIds, aws.String(id.(string)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vpcSettings := directoryservice.DirectoryVpcSettings{
|
|
|
|
|
SubnetIds: subnetIds,
|
|
|
|
|
VpcId: aws.String(s["vpc_id"].(string)),
|
|
|
|
|
var customerDnsIps []*string
|
|
|
|
|
for _, id := range s["customer_dns_ips"].(*schema.Set).List() {
|
|
|
|
|
customerDnsIps = append(customerDnsIps, aws.String(id.(string)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connectSettings = &directoryservice.DirectoryConnectSettings{
|
|
|
|
|
CustomerDnsIps: customerDnsIps,
|
|
|
|
|
CustomerUserName: aws.String(s["customer_username"].(string)),
|
|
|
|
|
SubnetIds: subnetIds,
|
|
|
|
|
VpcId: aws.String(s["vpc_id"].(string)),
|
|
|
|
|
}
|
|
|
|
|
input.VpcSettings = &vpcSettings
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating Directory Service: %s", input)
|
|
|
|
|
return connectSettings, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createDirectoryConnector(dsconn *directoryservice.DirectoryService, d *schema.ResourceData) (directoryId string, err error) {
|
|
|
|
|
if _, ok := d.GetOk("size"); !ok {
|
|
|
|
|
return "", fmt.Errorf("size is required for type = ADConnector")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input := directoryservice.ConnectDirectoryInput{
|
|
|
|
|
Name: aws.String(d.Get("name").(string)),
|
|
|
|
|
Password: aws.String(d.Get("password").(string)),
|
|
|
|
|
Size: aws.String(d.Get("size").(string)),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
|
input.Description = aws.String(v.(string))
|
|
|
|
|
}
|
|
|
|
|
if v, ok := d.GetOk("short_name"); ok {
|
|
|
|
|
input.ShortName = aws.String(v.(string))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input.ConnectSettings, err = buildConnectSettings(d)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating Directory Connector: %s", input)
|
|
|
|
|
out, err := dsconn.ConnectDirectory(&input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
log.Printf("[DEBUG] Directory Connector created: %s", out)
|
|
|
|
|
|
|
|
|
|
return *out.DirectoryId, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createSimpleDirectoryService(dsconn *directoryservice.DirectoryService, d *schema.ResourceData) (directoryId string, err error) {
|
|
|
|
|
if _, ok := d.GetOk("size"); !ok {
|
|
|
|
|
return "", fmt.Errorf("size is required for type = SimpleAD")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input := directoryservice.CreateDirectoryInput{
|
|
|
|
|
Name: aws.String(d.Get("name").(string)),
|
|
|
|
|
Password: aws.String(d.Get("password").(string)),
|
|
|
|
|
Size: aws.String(d.Get("size").(string)),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
|
input.Description = aws.String(v.(string))
|
|
|
|
|
}
|
|
|
|
|
if v, ok := d.GetOk("short_name"); ok {
|
|
|
|
|
input.ShortName = aws.String(v.(string))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input.VpcSettings, err = buildVpcSettings(d)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating Simple Directory Service: %s", input)
|
|
|
|
|
out, err := dsconn.CreateDirectory(&input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
log.Printf("[DEBUG] Simple Directory Service created: %s", out)
|
|
|
|
|
|
|
|
|
|
return *out.DirectoryId, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createActiveDirectoryService(dsconn *directoryservice.DirectoryService, d *schema.ResourceData) (directoryId string, err error) {
|
|
|
|
|
input := directoryservice.CreateMicrosoftADInput{
|
|
|
|
|
Name: aws.String(d.Get("name").(string)),
|
|
|
|
|
Password: aws.String(d.Get("password").(string)),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
|
input.Description = aws.String(v.(string))
|
|
|
|
|
}
|
|
|
|
|
if v, ok := d.GetOk("short_name"); ok {
|
|
|
|
|
input.ShortName = aws.String(v.(string))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input.VpcSettings, err = buildVpcSettings(d)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating Microsoft AD Directory Service: %s", input)
|
|
|
|
|
out, err := dsconn.CreateMicrosoftAD(&input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
log.Printf("[DEBUG] Microsoft AD Directory Service created: %s", out)
|
|
|
|
|
|
|
|
|
|
return *out.DirectoryId, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resourceAwsDirectoryServiceDirectoryCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
|
dsconn := meta.(*AWSClient).dsconn
|
|
|
|
|
|
|
|
|
|
creationFunc, ok := directoryCreationFuncs[d.Get("type").(string)]
|
|
|
|
|
if !ok {
|
|
|
|
|
// Shouldn't happen as this is validated above
|
|
|
|
|
return fmt.Errorf("Unsupported directory type: %s", d.Get("type"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
directoryId, err := creationFunc(dsconn, d)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
log.Printf("[DEBUG] Directory Service created: %s", out)
|
|
|
|
|
d.SetId(*out.DirectoryId)
|
|
|
|
|
|
|
|
|
|
d.SetId(directoryId)
|
|
|
|
|
|
|
|
|
|
// Wait for creation
|
|
|
|
|
log.Printf("[DEBUG] Waiting for DS (%q) to become available", d.Id())
|
|
|
|
|
@ -159,7 +336,7 @@ func resourceAwsDirectoryServiceDirectoryCreate(d *schema.ResourceData, meta int
|
|
|
|
|
d.Id(), *ds.Stage)
|
|
|
|
|
return ds, *ds.Stage, nil
|
|
|
|
|
},
|
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
|
Timeout: 30 * time.Minute,
|
|
|
|
|
}
|
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
|
return fmt.Errorf(
|
|
|
|
|
@ -234,14 +411,22 @@ func resourceAwsDirectoryServiceDirectoryRead(d *schema.ResourceData, meta inter
|
|
|
|
|
if dir.Description != nil {
|
|
|
|
|
d.Set("description", *dir.Description)
|
|
|
|
|
}
|
|
|
|
|
d.Set("dns_ip_addresses", schema.NewSet(schema.HashString, flattenStringList(dir.DnsIpAddrs)))
|
|
|
|
|
|
|
|
|
|
if *dir.Type == "ADConnector" {
|
|
|
|
|
d.Set("dns_ip_addresses", schema.NewSet(schema.HashString, flattenStringList(dir.ConnectSettings.ConnectIps)))
|
|
|
|
|
} else {
|
|
|
|
|
d.Set("dns_ip_addresses", schema.NewSet(schema.HashString, flattenStringList(dir.DnsIpAddrs)))
|
|
|
|
|
}
|
|
|
|
|
d.Set("name", *dir.Name)
|
|
|
|
|
if dir.ShortName != nil {
|
|
|
|
|
d.Set("short_name", *dir.ShortName)
|
|
|
|
|
}
|
|
|
|
|
d.Set("size", *dir.Size)
|
|
|
|
|
if dir.Size != nil {
|
|
|
|
|
d.Set("size", *dir.Size)
|
|
|
|
|
}
|
|
|
|
|
d.Set("type", *dir.Type)
|
|
|
|
|
d.Set("vpc_settings", flattenDSVpcSettings(dir.VpcSettings))
|
|
|
|
|
d.Set("connect_settings", flattenDSConnectSettings(dir.DnsIpAddrs, dir.ConnectSettings))
|
|
|
|
|
d.Set("enable_sso", *dir.SsoEnabled)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
@ -285,7 +470,7 @@ func resourceAwsDirectoryServiceDirectoryDelete(d *schema.ResourceData, meta int
|
|
|
|
|
d.Id(), *ds.Stage)
|
|
|
|
|
return ds, *ds.Stage, nil
|
|
|
|
|
},
|
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
|
Timeout: 30 * time.Minute,
|
|
|
|
|
}
|
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
|
return fmt.Errorf(
|
|
|
|
|
|