github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_opsworks_permission.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/opsworks"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsOpsworksPermission() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsOpsworksPermissionCreate,
    18  		Update: resourceAwsOpsworksPermissionCreate,
    19  		Delete: resourceAwsOpsworksPermissionDelete,
    20  		Read:   resourceAwsOpsworksPermissionRead,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"id": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Computed: true,
    26  			},
    27  			"allow_ssh": &schema.Schema{
    28  				Type:     schema.TypeBool,
    29  				Computed: true,
    30  				Optional: true,
    31  			},
    32  			"allow_sudo": &schema.Schema{
    33  				Type:     schema.TypeBool,
    34  				Computed: true,
    35  				Optional: true,
    36  			},
    37  			"user_arn": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  			},
    41  			// one of deny, show, deploy, manage, iam_only
    42  			"level": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Computed: true,
    45  				Optional: true,
    46  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    47  					value := v.(string)
    48  
    49  					expected := [5]string{"deny", "show", "deploy", "manage", "iam_only"}
    50  
    51  					found := false
    52  					for _, b := range expected {
    53  						if b == value {
    54  							found = true
    55  						}
    56  					}
    57  					if !found {
    58  						errors = append(errors, fmt.Errorf(
    59  							"%q has to be one of [deny, show, deploy, manage, iam_only]", k))
    60  					}
    61  					return
    62  				},
    63  			},
    64  			"stack_id": &schema.Schema{
    65  				Type:     schema.TypeString,
    66  				Computed: true,
    67  				Optional: true,
    68  			},
    69  		},
    70  	}
    71  }
    72  
    73  func resourceAwsOpsworksPermissionDelete(d *schema.ResourceData, meta interface{}) error {
    74  	return nil
    75  }
    76  
    77  func resourceAwsOpsworksPermissionRead(d *schema.ResourceData, meta interface{}) error {
    78  	client := meta.(*AWSClient).opsworksconn
    79  
    80  	req := &opsworks.DescribePermissionsInput{
    81  		IamUserArn: aws.String(d.Get("user_arn").(string)),
    82  		StackId:    aws.String(d.Get("stack_id").(string)),
    83  	}
    84  
    85  	log.Printf("[DEBUG] Reading OpsWorks prermissions for: %s on stack: %s", d.Get("user_arn"), d.Get("stack_id"))
    86  
    87  	resp, err := client.DescribePermissions(req)
    88  	if err != nil {
    89  		if awserr, ok := err.(awserr.Error); ok {
    90  			if awserr.Code() == "ResourceNotFoundException" {
    91  				log.Printf("[INFO] Permission not found")
    92  				d.SetId("")
    93  				return nil
    94  			}
    95  		}
    96  		return err
    97  	}
    98  
    99  	found := false
   100  	id := ""
   101  	for _, permission := range resp.Permissions {
   102  		id = *permission.IamUserArn + *permission.StackId
   103  
   104  		if d.Get("user_arn").(string)+d.Get("stack_id").(string) == id {
   105  			found = true
   106  			d.SetId(id)
   107  			d.Set("id", id)
   108  			d.Set("allow_ssh", permission.AllowSudo)
   109  			d.Set("allow_sodo", permission.AllowSudo)
   110  			d.Set("user_arn", permission.IamUserArn)
   111  			d.Set("stack_id", permission.StackId)
   112  		}
   113  
   114  	}
   115  
   116  	if false == found {
   117  		d.SetId("")
   118  		log.Printf("[INFO] The correct permission could not be found for: %s on stack: %s", d.Get("user_arn"), d.Get("stack_id"))
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  func resourceAwsOpsworksPermissionCreate(d *schema.ResourceData, meta interface{}) error {
   125  	client := meta.(*AWSClient).opsworksconn
   126  
   127  	req := &opsworks.SetPermissionInput{
   128  		AllowSudo:  aws.Bool(d.Get("allow_sudo").(bool)),
   129  		AllowSsh:   aws.Bool(d.Get("allow_ssh").(bool)),
   130  		IamUserArn: aws.String(d.Get("user_arn").(string)),
   131  		StackId:    aws.String(d.Get("stack_id").(string)),
   132  	}
   133  
   134  	err := resource.Retry(2*time.Minute, func() *resource.RetryError {
   135  		var cerr error
   136  		_, cerr = client.SetPermission(req)
   137  		if cerr != nil {
   138  			log.Printf("[INFO] client error")
   139  			if opserr, ok := cerr.(awserr.Error); ok {
   140  				// XXX: handle errors
   141  				log.Printf("[ERROR] OpsWorks error: %s message: %s", opserr.Code(), opserr.Message())
   142  				return resource.RetryableError(cerr)
   143  			}
   144  			return resource.NonRetryableError(cerr)
   145  		}
   146  		return nil
   147  	})
   148  
   149  	if err != nil {
   150  		return err
   151  	}
   152  
   153  	return resourceAwsOpsworksPermissionRead(d, meta)
   154  }