github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_codecommit_trigger.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/codecommit" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func resourceAwsCodeCommitTrigger() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceAwsCodeCommitTriggerCreate, 15 Read: resourceAwsCodeCommitTriggerRead, 16 Delete: resourceAwsCodeCommitTriggerDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "repository_name": &schema.Schema{ 20 Type: schema.TypeString, 21 ForceNew: true, 22 Required: true, 23 }, 24 "configuration_id": &schema.Schema{ 25 Type: schema.TypeString, 26 Computed: true, 27 }, 28 "trigger": &schema.Schema{ 29 Type: schema.TypeSet, 30 ForceNew: true, 31 Required: true, 32 MaxItems: 10, 33 Elem: &schema.Resource{ 34 Schema: map[string]*schema.Schema{ 35 "name": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 ForceNew: true, 39 }, 40 41 "destination_arn": &schema.Schema{ 42 Type: schema.TypeString, 43 Required: true, 44 ForceNew: true, 45 }, 46 47 "custom_data": &schema.Schema{ 48 Type: schema.TypeString, 49 Optional: true, 50 ForceNew: true, 51 }, 52 53 "branches": &schema.Schema{ 54 Type: schema.TypeList, 55 Optional: true, 56 ForceNew: true, 57 Elem: &schema.Schema{Type: schema.TypeString}, 58 }, 59 60 "events": &schema.Schema{ 61 Type: schema.TypeList, 62 Required: true, 63 ForceNew: true, 64 Elem: &schema.Schema{Type: schema.TypeString}, 65 }, 66 }, 67 }, 68 }, 69 }, 70 } 71 } 72 73 func resourceAwsCodeCommitTriggerCreate(d *schema.ResourceData, meta interface{}) error { 74 conn := meta.(*AWSClient).codecommitconn 75 76 // Expand the "trigger" set to aws-sdk-go compat []*codecommit.RepositoryTrigger 77 triggers := expandAwsCodeCommitTriggers(d.Get("trigger").(*schema.Set).List()) 78 79 input := &codecommit.PutRepositoryTriggersInput{ 80 RepositoryName: aws.String(d.Get("repository_name").(string)), 81 Triggers: triggers, 82 } 83 84 resp, err := conn.PutRepositoryTriggers(input) 85 if err != nil { 86 return fmt.Errorf("Error creating CodeCommit Trigger: %s", err) 87 } 88 89 log.Printf("[INFO] Code Commit Trigger Created %s input %s", resp, input) 90 91 d.SetId(d.Get("repository_name").(string)) 92 d.Set("configuration_id", resp.ConfigurationId) 93 94 return resourceAwsCodeCommitTriggerRead(d, meta) 95 } 96 97 func resourceAwsCodeCommitTriggerRead(d *schema.ResourceData, meta interface{}) error { 98 conn := meta.(*AWSClient).codecommitconn 99 100 input := &codecommit.GetRepositoryTriggersInput{ 101 RepositoryName: aws.String(d.Id()), 102 } 103 104 resp, err := conn.GetRepositoryTriggers(input) 105 if err != nil { 106 return fmt.Errorf("Error reading CodeCommit Trigger: %s", err.Error()) 107 } 108 109 log.Printf("[DEBUG] CodeCommit Trigger: %s", resp) 110 111 return nil 112 } 113 114 func resourceAwsCodeCommitTriggerDelete(d *schema.ResourceData, meta interface{}) error { 115 116 conn := meta.(*AWSClient).codecommitconn 117 118 log.Printf("[DEBUG] Deleting Trigger: %q", d.Id()) 119 120 input := &codecommit.PutRepositoryTriggersInput{ 121 RepositoryName: aws.String(d.Get("repository_name").(string)), 122 Triggers: []*codecommit.RepositoryTrigger{}, 123 } 124 125 _, err := conn.PutRepositoryTriggers(input) 126 127 if err != nil { 128 return err 129 } 130 131 return nil 132 } 133 134 func expandAwsCodeCommitTriggers(configured []interface{}) []*codecommit.RepositoryTrigger { 135 triggers := make([]*codecommit.RepositoryTrigger, 0, len(configured)) 136 // Loop over our configured triggers and create 137 // an array of aws-sdk-go compatabile objects 138 for _, lRaw := range configured { 139 data := lRaw.(map[string]interface{}) 140 t := &codecommit.RepositoryTrigger{ 141 CustomData: aws.String(data["custom_data"].(string)), 142 DestinationArn: aws.String(data["destination_arn"].(string)), 143 Name: aws.String(data["name"].(string)), 144 } 145 146 branches := make([]*string, len(data["events"].([]interface{}))) 147 for i, vv := range data["branches"].([]interface{}) { 148 str := vv.(string) 149 branches[i] = aws.String(str) 150 } 151 t.Branches = branches 152 153 events := make([]*string, len(data["events"].([]interface{}))) 154 for i, vv := range data["events"].([]interface{}) { 155 str := vv.(string) 156 events[i] = aws.String(str) 157 } 158 t.Events = events 159 160 triggers = append(triggers, t) 161 } 162 return triggers 163 }