github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_directory_service_directory.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/service/directoryservice"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  )
    14  
    15  func resourceAwsDirectoryServiceDirectory() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsDirectoryServiceDirectoryCreate,
    18  		Read:   resourceAwsDirectoryServiceDirectoryRead,
    19  		Update: resourceAwsDirectoryServiceDirectoryUpdate,
    20  		Delete: resourceAwsDirectoryServiceDirectoryDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  			"password": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  			"size": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  			"alias": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Optional: true,
    41  				Computed: true,
    42  				ForceNew: true,
    43  			},
    44  			"description": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Optional: true,
    47  				ForceNew: true,
    48  			},
    49  			"short_name": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Optional: true,
    52  				Computed: true,
    53  				ForceNew: true,
    54  			},
    55  			"vpc_settings": &schema.Schema{
    56  				Type:     schema.TypeList,
    57  				Required: true,
    58  				Elem: &schema.Resource{
    59  					Schema: map[string]*schema.Schema{
    60  						"subnet_ids": &schema.Schema{
    61  							Type:     schema.TypeSet,
    62  							Required: true,
    63  							ForceNew: true,
    64  							Elem:     &schema.Schema{Type: schema.TypeString},
    65  							Set:      schema.HashString,
    66  						},
    67  						"vpc_id": &schema.Schema{
    68  							Type:     schema.TypeString,
    69  							Required: true,
    70  							ForceNew: true,
    71  						},
    72  					},
    73  				},
    74  			},
    75  			"enable_sso": &schema.Schema{
    76  				Type:     schema.TypeBool,
    77  				Optional: true,
    78  				Default:  false,
    79  			},
    80  			"access_url": &schema.Schema{
    81  				Type:     schema.TypeString,
    82  				Computed: true,
    83  			},
    84  			"dns_ip_addresses": &schema.Schema{
    85  				Type:     schema.TypeSet,
    86  				Elem:     &schema.Schema{Type: schema.TypeString},
    87  				Set:      schema.HashString,
    88  				Computed: true,
    89  			},
    90  			"type": &schema.Schema{
    91  				Type:     schema.TypeString,
    92  				Computed: true,
    93  			},
    94  		},
    95  	}
    96  }
    97  
    98  func resourceAwsDirectoryServiceDirectoryCreate(d *schema.ResourceData, meta interface{}) error {
    99  	dsconn := meta.(*AWSClient).dsconn
   100  
   101  	input := directoryservice.CreateDirectoryInput{
   102  		Name:     aws.String(d.Get("name").(string)),
   103  		Password: aws.String(d.Get("password").(string)),
   104  		Size:     aws.String(d.Get("size").(string)),
   105  	}
   106  
   107  	if v, ok := d.GetOk("description"); ok {
   108  		input.Description = aws.String(v.(string))
   109  	}
   110  	if v, ok := d.GetOk("short_name"); ok {
   111  		input.ShortName = aws.String(v.(string))
   112  	}
   113  
   114  	if v, ok := d.GetOk("vpc_settings"); ok {
   115  		settings := v.([]interface{})
   116  
   117  		if len(settings) > 1 {
   118  			return fmt.Errorf("Only a single vpc_settings block is expected")
   119  		} else if len(settings) == 1 {
   120  			s := settings[0].(map[string]interface{})
   121  			var subnetIds []*string
   122  			for _, id := range s["subnet_ids"].(*schema.Set).List() {
   123  				subnetIds = append(subnetIds, aws.String(id.(string)))
   124  			}
   125  
   126  			vpcSettings := directoryservice.DirectoryVpcSettings{
   127  				SubnetIds: subnetIds,
   128  				VpcId:     aws.String(s["vpc_id"].(string)),
   129  			}
   130  			input.VpcSettings = &vpcSettings
   131  		}
   132  	}
   133  
   134  	log.Printf("[DEBUG] Creating Directory Service: %s", input)
   135  	out, err := dsconn.CreateDirectory(&input)
   136  	if err != nil {
   137  		return err
   138  	}
   139  	log.Printf("[DEBUG] Directory Service created: %s", out)
   140  	d.SetId(*out.DirectoryId)
   141  
   142  	// Wait for creation
   143  	log.Printf("[DEBUG] Waiting for DS (%q) to become available", d.Id())
   144  	stateConf := &resource.StateChangeConf{
   145  		Pending: []string{"Requested", "Creating", "Created"},
   146  		Target:  "Active",
   147  		Refresh: func() (interface{}, string, error) {
   148  			resp, err := dsconn.DescribeDirectories(&directoryservice.DescribeDirectoriesInput{
   149  				DirectoryIds: []*string{aws.String(d.Id())},
   150  			})
   151  			if err != nil {
   152  				log.Printf("Error during creation of DS: %q", err.Error())
   153  				return nil, "", err
   154  			}
   155  
   156  			ds := resp.DirectoryDescriptions[0]
   157  			log.Printf("[DEBUG] Creation of DS %q is in following stage: %q.",
   158  				d.Id(), *ds.Stage)
   159  			return ds, *ds.Stage, nil
   160  		},
   161  		Timeout: 10 * time.Minute,
   162  	}
   163  	if _, err := stateConf.WaitForState(); err != nil {
   164  		return fmt.Errorf(
   165  			"Error waiting for Directory Service (%s) to become available: %#v",
   166  			d.Id(), err)
   167  	}
   168  
   169  	if v, ok := d.GetOk("alias"); ok {
   170  		d.SetPartial("alias")
   171  
   172  		input := directoryservice.CreateAliasInput{
   173  			DirectoryId: aws.String(d.Id()),
   174  			Alias:       aws.String(v.(string)),
   175  		}
   176  
   177  		log.Printf("[DEBUG] Assigning alias %q to DS directory %q",
   178  			v.(string), d.Id())
   179  		out, err := dsconn.CreateAlias(&input)
   180  		if err != nil {
   181  			return err
   182  		}
   183  		log.Printf("[DEBUG] Alias %q assigned to DS directory %q",
   184  			*out.Alias, *out.DirectoryId)
   185  	}
   186  
   187  	return resourceAwsDirectoryServiceDirectoryUpdate(d, meta)
   188  }
   189  
   190  func resourceAwsDirectoryServiceDirectoryUpdate(d *schema.ResourceData, meta interface{}) error {
   191  	dsconn := meta.(*AWSClient).dsconn
   192  
   193  	if d.HasChange("enable_sso") {
   194  		d.SetPartial("enable_sso")
   195  		var err error
   196  
   197  		if v, ok := d.GetOk("enable_sso"); ok && v.(bool) {
   198  			log.Printf("[DEBUG] Enabling SSO for DS directory %q", d.Id())
   199  			_, err = dsconn.EnableSso(&directoryservice.EnableSsoInput{
   200  				DirectoryId: aws.String(d.Id()),
   201  			})
   202  		} else {
   203  			log.Printf("[DEBUG] Disabling SSO for DS directory %q", d.Id())
   204  			_, err = dsconn.DisableSso(&directoryservice.DisableSsoInput{
   205  				DirectoryId: aws.String(d.Id()),
   206  			})
   207  		}
   208  
   209  		if err != nil {
   210  			return err
   211  		}
   212  	}
   213  
   214  	return resourceAwsDirectoryServiceDirectoryRead(d, meta)
   215  }
   216  
   217  func resourceAwsDirectoryServiceDirectoryRead(d *schema.ResourceData, meta interface{}) error {
   218  	dsconn := meta.(*AWSClient).dsconn
   219  
   220  	input := directoryservice.DescribeDirectoriesInput{
   221  		DirectoryIds: []*string{aws.String(d.Id())},
   222  	}
   223  	out, err := dsconn.DescribeDirectories(&input)
   224  	if err != nil {
   225  		return err
   226  	}
   227  
   228  	dir := out.DirectoryDescriptions[0]
   229  	log.Printf("[DEBUG] Received DS directory: %s", *dir)
   230  
   231  	d.Set("access_url", *dir.AccessUrl)
   232  	d.Set("alias", *dir.Alias)
   233  	if dir.Description != nil {
   234  		d.Set("description", *dir.Description)
   235  	}
   236  	d.Set("dns_ip_addresses", schema.NewSet(schema.HashString, flattenStringList(dir.DnsIpAddrs)))
   237  	d.Set("name", *dir.Name)
   238  	if dir.ShortName != nil {
   239  		d.Set("short_name", *dir.ShortName)
   240  	}
   241  	d.Set("size", *dir.Size)
   242  	d.Set("type", *dir.Type)
   243  	d.Set("vpc_settings", flattenDSVpcSettings(dir.VpcSettings))
   244  	d.Set("enable_sso", *dir.SsoEnabled)
   245  
   246  	return nil
   247  }
   248  
   249  func resourceAwsDirectoryServiceDirectoryDelete(d *schema.ResourceData, meta interface{}) error {
   250  	dsconn := meta.(*AWSClient).dsconn
   251  
   252  	input := directoryservice.DeleteDirectoryInput{
   253  		DirectoryId: aws.String(d.Id()),
   254  	}
   255  	_, err := dsconn.DeleteDirectory(&input)
   256  	if err != nil {
   257  		return err
   258  	}
   259  
   260  	// Wait for deletion
   261  	log.Printf("[DEBUG] Waiting for DS (%q) to be deleted", d.Id())
   262  	stateConf := &resource.StateChangeConf{
   263  		Pending: []string{"Deleting"},
   264  		Target:  "",
   265  		Refresh: func() (interface{}, string, error) {
   266  			resp, err := dsconn.DescribeDirectories(&directoryservice.DescribeDirectoriesInput{
   267  				DirectoryIds: []*string{aws.String(d.Id())},
   268  			})
   269  			if err != nil {
   270  				return nil, "", err
   271  			}
   272  
   273  			if len(resp.DirectoryDescriptions) == 0 {
   274  				return nil, "", nil
   275  			}
   276  
   277  			ds := resp.DirectoryDescriptions[0]
   278  			log.Printf("[DEBUG] Deletion of DS %q is in following stage: %q.",
   279  				d.Id(), *ds.Stage)
   280  			return ds, *ds.Stage, nil
   281  		},
   282  		Timeout: 10 * time.Minute,
   283  	}
   284  	if _, err := stateConf.WaitForState(); err != nil {
   285  		return fmt.Errorf(
   286  			"Error waiting for Directory Service (%s) to be deleted: %q",
   287  			d.Id(), err.Error())
   288  	}
   289  
   290  	return nil
   291  }