github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/aws/resource_aws_ssm_maintenance_window_task.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ssm"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceAwsSsmMaintenanceWindowTask() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsSsmMaintenanceWindowTaskCreate,
    15  		Read:   resourceAwsSsmMaintenanceWindowTaskRead,
    16  		Delete: resourceAwsSsmMaintenanceWindowTaskDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"window_id": {
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"max_concurrency": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"max_errors": {
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"task_type": {
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  
    43  			"task_arn": {
    44  				Type:     schema.TypeString,
    45  				Required: true,
    46  				ForceNew: true,
    47  			},
    48  
    49  			"service_role_arn": {
    50  				Type:     schema.TypeString,
    51  				Required: true,
    52  				ForceNew: true,
    53  			},
    54  
    55  			"targets": {
    56  				Type:     schema.TypeList,
    57  				Required: true,
    58  				ForceNew: true,
    59  				Elem: &schema.Resource{
    60  					Schema: map[string]*schema.Schema{
    61  						"key": {
    62  							Type:     schema.TypeString,
    63  							Required: true,
    64  						},
    65  						"values": {
    66  							Type:     schema.TypeList,
    67  							Required: true,
    68  							Elem:     &schema.Schema{Type: schema.TypeString},
    69  						},
    70  					},
    71  				},
    72  			},
    73  
    74  			"priority": {
    75  				Type:     schema.TypeInt,
    76  				Optional: true,
    77  				ForceNew: true,
    78  			},
    79  
    80  			"logging_info": {
    81  				Type:     schema.TypeList,
    82  				MaxItems: 1,
    83  				Optional: true,
    84  				ForceNew: true,
    85  				Elem: &schema.Resource{
    86  					Schema: map[string]*schema.Schema{
    87  						"s3_bucket_name": {
    88  							Type:     schema.TypeString,
    89  							Required: true,
    90  						},
    91  						"s3_region": {
    92  							Type:     schema.TypeString,
    93  							Required: true,
    94  						},
    95  						"s3_bucket_prefix": {
    96  							Type:     schema.TypeString,
    97  							Optional: true,
    98  						},
    99  					},
   100  				},
   101  			},
   102  		},
   103  	}
   104  }
   105  
   106  func expandAwsSsmMaintenanceWindowLoggingInfo(config []interface{}) *ssm.LoggingInfo {
   107  
   108  	loggingConfig := config[0].(map[string]interface{})
   109  
   110  	loggingInfo := &ssm.LoggingInfo{
   111  		S3BucketName: aws.String(loggingConfig["s3_bucket_name"].(string)),
   112  		S3Region:     aws.String(loggingConfig["s3_region"].(string)),
   113  	}
   114  
   115  	if s := loggingConfig["s3_bucket_prefix"].(string); s != "" {
   116  		loggingInfo.S3KeyPrefix = aws.String(s)
   117  	}
   118  
   119  	return loggingInfo
   120  }
   121  
   122  func flattenAwsSsmMaintenanceWindowLoggingInfo(loggingInfo *ssm.LoggingInfo) []interface{} {
   123  
   124  	result := make(map[string]interface{})
   125  	result["s3_bucket_name"] = *loggingInfo.S3BucketName
   126  	result["s3_region"] = *loggingInfo.S3Region
   127  
   128  	if loggingInfo.S3KeyPrefix != nil {
   129  		result["s3_bucket_prefix"] = *loggingInfo.S3KeyPrefix
   130  	}
   131  
   132  	return []interface{}{result}
   133  }
   134  
   135  func resourceAwsSsmMaintenanceWindowTaskCreate(d *schema.ResourceData, meta interface{}) error {
   136  	ssmconn := meta.(*AWSClient).ssmconn
   137  
   138  	log.Printf("[INFO] Registering SSM Maintenance Window Task")
   139  
   140  	params := &ssm.RegisterTaskWithMaintenanceWindowInput{
   141  		WindowId:       aws.String(d.Get("window_id").(string)),
   142  		MaxConcurrency: aws.String(d.Get("max_concurrency").(string)),
   143  		MaxErrors:      aws.String(d.Get("max_errors").(string)),
   144  		TaskType:       aws.String(d.Get("task_type").(string)),
   145  		ServiceRoleArn: aws.String(d.Get("service_role_arn").(string)),
   146  		TaskArn:        aws.String(d.Get("task_arn").(string)),
   147  		Targets:        expandAwsSsmTargets(d),
   148  	}
   149  
   150  	if v, ok := d.GetOk("priority"); ok {
   151  		params.Priority = aws.Int64(int64(v.(int)))
   152  	}
   153  
   154  	if v, ok := d.GetOk("logging_info"); ok {
   155  		params.LoggingInfo = expandAwsSsmMaintenanceWindowLoggingInfo(v.([]interface{}))
   156  	}
   157  
   158  	resp, err := ssmconn.RegisterTaskWithMaintenanceWindow(params)
   159  	if err != nil {
   160  		return err
   161  	}
   162  
   163  	d.SetId(*resp.WindowTaskId)
   164  
   165  	return resourceAwsSsmMaintenanceWindowTaskRead(d, meta)
   166  }
   167  
   168  func resourceAwsSsmMaintenanceWindowTaskRead(d *schema.ResourceData, meta interface{}) error {
   169  	ssmconn := meta.(*AWSClient).ssmconn
   170  
   171  	params := &ssm.DescribeMaintenanceWindowTasksInput{
   172  		WindowId: aws.String(d.Get("window_id").(string)),
   173  	}
   174  
   175  	resp, err := ssmconn.DescribeMaintenanceWindowTasks(params)
   176  	if err != nil {
   177  		return err
   178  	}
   179  
   180  	found := false
   181  	for _, t := range resp.Tasks {
   182  		if *t.WindowTaskId == d.Id() {
   183  			found = true
   184  
   185  			d.Set("window_id", t.WindowId)
   186  			d.Set("max_concurrency", t.MaxConcurrency)
   187  			d.Set("max_errors", t.MaxErrors)
   188  			d.Set("task_type", t.Type)
   189  			d.Set("service_role_arn", t.ServiceRoleArn)
   190  			d.Set("task_arn", t.TaskArn)
   191  			d.Set("priority", t.Priority)
   192  
   193  			if t.LoggingInfo != nil {
   194  				if err := d.Set("logging_info", flattenAwsSsmMaintenanceWindowLoggingInfo(t.LoggingInfo)); err != nil {
   195  					return fmt.Errorf("[DEBUG] Error setting logging_info error: %#v", err)
   196  				}
   197  			}
   198  
   199  			if err := d.Set("targets", flattenAwsSsmTargets(t.Targets)); err != nil {
   200  				return fmt.Errorf("[DEBUG] Error setting targets error: %#v", err)
   201  			}
   202  		}
   203  	}
   204  
   205  	if !found {
   206  		log.Printf("[INFO] Maintenance Window Target not found. Removing from state")
   207  		d.SetId("")
   208  		return nil
   209  	}
   210  
   211  	return nil
   212  }
   213  
   214  func resourceAwsSsmMaintenanceWindowTaskDelete(d *schema.ResourceData, meta interface{}) error {
   215  	ssmconn := meta.(*AWSClient).ssmconn
   216  
   217  	log.Printf("[INFO] Deregistering SSM Maintenance Window Task: %s", d.Id())
   218  
   219  	params := &ssm.DeregisterTaskFromMaintenanceWindowInput{
   220  		WindowId:     aws.String(d.Get("window_id").(string)),
   221  		WindowTaskId: aws.String(d.Id()),
   222  	}
   223  
   224  	_, err := ssmconn.DeregisterTaskFromMaintenanceWindow(params)
   225  	if err != nil {
   226  		return err
   227  	}
   228  
   229  	return nil
   230  }