github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/rancher/resource_rancher_registry_credential.go (about) 1 package rancher 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/helper/schema" 10 rancherClient "github.com/rancher/go-rancher/client" 11 ) 12 13 func resourceRancherRegistryCredential() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceRancherRegistryCredentialCreate, 16 Read: resourceRancherRegistryCredentialRead, 17 Update: resourceRancherRegistryCredentialUpdate, 18 Delete: resourceRancherRegistryCredentialDelete, 19 Importer: &schema.ResourceImporter{ 20 State: schema.ImportStatePassthrough, 21 }, 22 23 Schema: map[string]*schema.Schema{ 24 "id": &schema.Schema{ 25 Type: schema.TypeString, 26 Computed: true, 27 }, 28 "name": &schema.Schema{ 29 Type: schema.TypeString, 30 Required: true, 31 }, 32 "description": &schema.Schema{ 33 Type: schema.TypeString, 34 Optional: true, 35 }, 36 "registry_id": &schema.Schema{ 37 Type: schema.TypeString, 38 Required: true, 39 ForceNew: true, 40 }, 41 "email": &schema.Schema{ 42 Type: schema.TypeString, 43 Required: true, 44 }, 45 "public_value": &schema.Schema{ 46 Type: schema.TypeString, 47 Required: true, 48 }, 49 "secret_value": &schema.Schema{ 50 Type: schema.TypeString, 51 Required: true, 52 ForceNew: true, 53 }, 54 }, 55 } 56 } 57 58 func resourceRancherRegistryCredentialCreate(d *schema.ResourceData, meta interface{}) error { 59 log.Printf("[INFO] Creating RegistryCredential: %s", d.Id()) 60 client, err := meta.(*Config).RegistryClient(d.Get("registry_id").(string)) 61 if err != nil { 62 return err 63 } 64 65 name := d.Get("name").(string) 66 description := d.Get("description").(string) 67 email := d.Get("email").(string) 68 publicValue := d.Get("public_value").(string) 69 secretValue := d.Get("secret_value").(string) 70 registryID := d.Get("registry_id").(string) 71 72 registryCred := rancherClient.RegistryCredential{ 73 Name: name, 74 Description: description, 75 Email: email, 76 PublicValue: publicValue, 77 SecretValue: secretValue, 78 RegistryId: registryID, 79 } 80 newRegistryCredential, err := client.RegistryCredential.Create(®istryCred) 81 if err != nil { 82 return err 83 } 84 85 stateConf := &resource.StateChangeConf{ 86 Pending: []string{"active", "removed", "removing"}, 87 Target: []string{"active"}, 88 Refresh: RegistryCredentialStateRefreshFunc(client, newRegistryCredential.Id), 89 Timeout: 10 * time.Minute, 90 Delay: 1 * time.Second, 91 MinTimeout: 3 * time.Second, 92 } 93 _, waitErr := stateConf.WaitForState() 94 if waitErr != nil { 95 return fmt.Errorf( 96 "Error waiting for registry credential (%s) to be created: %s", newRegistryCredential.Id, waitErr) 97 } 98 99 d.SetId(newRegistryCredential.Id) 100 log.Printf("[INFO] RegistryCredential ID: %s", d.Id()) 101 102 return resourceRancherRegistryCredentialRead(d, meta) 103 } 104 105 func resourceRancherRegistryCredentialRead(d *schema.ResourceData, meta interface{}) error { 106 log.Printf("[INFO] Refreshing RegistryCredential: %s", d.Id()) 107 client := meta.(*Config) 108 109 registryCred, err := client.RegistryCredential.ById(d.Id()) 110 if err != nil { 111 return err 112 } 113 114 log.Printf("[INFO] RegistryCredential Name: %s", registryCred.Name) 115 116 d.Set("description", registryCred.Description) 117 d.Set("name", registryCred.Name) 118 d.Set("email", registryCred.Email) 119 d.Set("public_value", registryCred.PublicValue) 120 d.Set("registry_id", registryCred.RegistryId) 121 122 return nil 123 } 124 125 func resourceRancherRegistryCredentialUpdate(d *schema.ResourceData, meta interface{}) error { 126 log.Printf("[INFO] Updating RegistryCredential: %s", d.Id()) 127 client, err := meta.(*Config).RegistryClient(d.Get("registry_id").(string)) 128 if err != nil { 129 return err 130 } 131 132 registryCred, err := client.RegistryCredential.ById(d.Id()) 133 if err != nil { 134 return err 135 } 136 137 name := d.Get("name").(string) 138 description := d.Get("description").(string) 139 email := d.Get("email").(string) 140 publicValue := d.Get("public_value").(string) 141 secretValue := d.Get("secret_value").(string) 142 143 registryCred.Name = name 144 registryCred.Description = description 145 registryCred.Email = email 146 registryCred.PublicValue = publicValue 147 registryCred.SecretValue = secretValue 148 client.RegistryCredential.Update(registryCred, ®istryCred) 149 150 return resourceRancherRegistryCredentialRead(d, meta) 151 } 152 153 func resourceRancherRegistryCredentialDelete(d *schema.ResourceData, meta interface{}) error { 154 log.Printf("[INFO] Deleting RegistryCredential: %s", d.Id()) 155 id := d.Id() 156 client, err := meta.(*Config).RegistryClient(d.Get("registry_id").(string)) 157 if err != nil { 158 return err 159 } 160 161 reg, err := client.RegistryCredential.ById(id) 162 if err != nil { 163 return err 164 } 165 166 // Step 1: Deactivate 167 if _, e := client.RegistryCredential.ActionDeactivate(reg); e != nil { 168 return fmt.Errorf("Error deactivating RegistryCredential: %s", err) 169 } 170 171 log.Printf("[DEBUG] Waiting for registry credential (%s) to be deactivated", id) 172 173 stateConf := &resource.StateChangeConf{ 174 Pending: []string{"active", "inactive", "deactivating"}, 175 Target: []string{"inactive"}, 176 Refresh: RegistryCredentialStateRefreshFunc(client, id), 177 Timeout: 10 * time.Minute, 178 Delay: 1 * time.Second, 179 MinTimeout: 3 * time.Second, 180 } 181 182 _, waitErr := stateConf.WaitForState() 183 if waitErr != nil { 184 return fmt.Errorf( 185 "Error waiting for registry credential (%s) to be deactivated: %s", id, waitErr) 186 } 187 188 // Update resource to reflect its state 189 reg, err = client.RegistryCredential.ById(id) 190 if err != nil { 191 return fmt.Errorf("Failed to refresh state of deactivated registry credential (%s): %s", id, err) 192 } 193 194 // Step 2: Remove 195 if _, err := client.RegistryCredential.ActionRemove(reg); err != nil { 196 return fmt.Errorf("Error removing RegistryCredential: %s", err) 197 } 198 199 log.Printf("[DEBUG] Waiting for registry (%s) to be removed", id) 200 201 stateConf = &resource.StateChangeConf{ 202 Pending: []string{"inactive", "removed", "removing"}, 203 Target: []string{"removed"}, 204 Refresh: RegistryCredentialStateRefreshFunc(client, id), 205 Timeout: 10 * time.Minute, 206 Delay: 1 * time.Second, 207 MinTimeout: 3 * time.Second, 208 } 209 210 _, waitErr = stateConf.WaitForState() 211 if waitErr != nil { 212 return fmt.Errorf( 213 "Error waiting for registry (%s) to be removed: %s", id, waitErr) 214 } 215 216 d.SetId("") 217 return nil 218 } 219 220 // RegistryCredentialStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch 221 // a Rancher Environment. 222 func RegistryCredentialStateRefreshFunc(client *rancherClient.RancherClient, registryCredID string) resource.StateRefreshFunc { 223 return func() (interface{}, string, error) { 224 regC, err := client.RegistryCredential.ById(registryCredID) 225 226 if err != nil { 227 return nil, "", err 228 } 229 230 return regC, regC.State, nil 231 } 232 }