github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/rancher/resource_rancher_registry.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 resourceRancherRegistry() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceRancherRegistryCreate, 16 Read: resourceRancherRegistryRead, 17 Update: resourceRancherRegistryUpdate, 18 Delete: resourceRancherRegistryDelete, 19 Importer: &schema.ResourceImporter{ 20 State: resourceRancherRegistryImport, 21 }, 22 23 Schema: map[string]*schema.Schema{ 24 "id": &schema.Schema{ 25 Type: schema.TypeString, 26 Computed: true, 27 }, 28 "server_address": &schema.Schema{ 29 Type: schema.TypeString, 30 Required: true, 31 ForceNew: true, 32 }, 33 "name": &schema.Schema{ 34 Type: schema.TypeString, 35 Required: true, 36 }, 37 "description": &schema.Schema{ 38 Type: schema.TypeString, 39 Optional: true, 40 }, 41 "environment_id": &schema.Schema{ 42 Type: schema.TypeString, 43 Required: true, 44 ForceNew: true, 45 }, 46 }, 47 } 48 } 49 50 func resourceRancherRegistryCreate(d *schema.ResourceData, meta interface{}) error { 51 log.Printf("[INFO] Creating Registry: %s", d.Id()) 52 client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string)) 53 if err != nil { 54 return err 55 } 56 57 name := d.Get("name").(string) 58 description := d.Get("description").(string) 59 serverAddress := d.Get("server_address").(string) 60 61 registry := rancherClient.Registry{ 62 Name: name, 63 Description: description, 64 ServerAddress: serverAddress, 65 } 66 newRegistry, err := client.Registry.Create(®istry) 67 if err != nil { 68 return err 69 } 70 71 stateConf := &resource.StateChangeConf{ 72 Pending: []string{"active", "removed", "removing"}, 73 Target: []string{"active"}, 74 Refresh: RegistryStateRefreshFunc(client, newRegistry.Id), 75 Timeout: 10 * time.Minute, 76 Delay: 1 * time.Second, 77 MinTimeout: 3 * time.Second, 78 } 79 _, waitErr := stateConf.WaitForState() 80 if waitErr != nil { 81 return fmt.Errorf( 82 "Error waiting for registry (%s) to be created: %s", newRegistry.Id, waitErr) 83 } 84 85 d.SetId(newRegistry.Id) 86 log.Printf("[INFO] Registry ID: %s", d.Id()) 87 88 return resourceRancherRegistryRead(d, meta) 89 } 90 91 func resourceRancherRegistryRead(d *schema.ResourceData, meta interface{}) error { 92 log.Printf("[INFO] Refreshing Registry: %s", d.Id()) 93 client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string)) 94 if err != nil { 95 return err 96 } 97 98 registry, err := client.Registry.ById(d.Id()) 99 if err != nil { 100 return err 101 } 102 103 if registry == nil { 104 log.Printf("[INFO] Registry %s not found", d.Id()) 105 d.SetId("") 106 return nil 107 } 108 109 if removed(registry.State) { 110 log.Printf("[INFO] Registry %s was removed on %v", d.Id(), registry.Removed) 111 d.SetId("") 112 return nil 113 } 114 115 log.Printf("[INFO] Registry Name: %s", registry.Name) 116 117 d.Set("description", registry.Description) 118 d.Set("name", registry.Name) 119 d.Set("server_address", registry.ServerAddress) 120 d.Set("environment_id", registry.AccountId) 121 122 return nil 123 } 124 125 func resourceRancherRegistryUpdate(d *schema.ResourceData, meta interface{}) error { 126 client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string)) 127 if err != nil { 128 return err 129 } 130 131 registry, err := client.Registry.ById(d.Id()) 132 if err != nil { 133 return err 134 } 135 136 name := d.Get("name").(string) 137 description := d.Get("description").(string) 138 139 registry.Name = name 140 registry.Description = description 141 client.Registry.Update(registry, ®istry) 142 143 return resourceRancherRegistryRead(d, meta) 144 } 145 146 func resourceRancherRegistryDelete(d *schema.ResourceData, meta interface{}) error { 147 log.Printf("[INFO] Deleting Registry: %s", d.Id()) 148 id := d.Id() 149 client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string)) 150 if err != nil { 151 return err 152 } 153 154 reg, err := client.Registry.ById(id) 155 if err != nil { 156 return err 157 } 158 159 // Step 1: Deactivate 160 if _, e := client.Registry.ActionDeactivate(reg); e != nil { 161 return fmt.Errorf("Error deactivating Registry: %s", err) 162 } 163 164 log.Printf("[DEBUG] Waiting for registry (%s) to be deactivated", id) 165 166 stateConf := &resource.StateChangeConf{ 167 Pending: []string{"active", "inactive", "deactivating"}, 168 Target: []string{"inactive"}, 169 Refresh: RegistryStateRefreshFunc(client, id), 170 Timeout: 10 * time.Minute, 171 Delay: 1 * time.Second, 172 MinTimeout: 3 * time.Second, 173 } 174 175 _, waitErr := stateConf.WaitForState() 176 if waitErr != nil { 177 return fmt.Errorf( 178 "Error waiting for registry (%s) to be deactivated: %s", id, waitErr) 179 } 180 181 // Update resource to reflect its state 182 reg, err = client.Registry.ById(id) 183 if err != nil { 184 return fmt.Errorf("Failed to refresh state of deactivated registry (%s): %s", id, err) 185 } 186 187 // Step 2: Remove 188 if _, err := client.Registry.ActionRemove(reg); err != nil { 189 return fmt.Errorf("Error removing Registry: %s", err) 190 } 191 192 log.Printf("[DEBUG] Waiting for registry (%s) to be removed", id) 193 194 stateConf = &resource.StateChangeConf{ 195 Pending: []string{"inactive", "removed", "removing"}, 196 Target: []string{"removed"}, 197 Refresh: RegistryStateRefreshFunc(client, id), 198 Timeout: 10 * time.Minute, 199 Delay: 1 * time.Second, 200 MinTimeout: 3 * time.Second, 201 } 202 203 _, waitErr = stateConf.WaitForState() 204 if waitErr != nil { 205 return fmt.Errorf( 206 "Error waiting for registry (%s) to be removed: %s", id, waitErr) 207 } 208 209 d.SetId("") 210 return nil 211 } 212 213 func resourceRancherRegistryImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 214 envID, resourceID := splitID(d.Id()) 215 d.SetId(resourceID) 216 if envID != "" { 217 d.Set("environment_id", envID) 218 } else { 219 client, err := meta.(*Config).GlobalClient() 220 if err != nil { 221 return []*schema.ResourceData{}, err 222 } 223 registry, err := client.Registry.ById(d.Id()) 224 if err != nil { 225 return []*schema.ResourceData{}, err 226 } 227 d.Set("environment_id", registry.AccountId) 228 } 229 return []*schema.ResourceData{d}, nil 230 } 231 232 // RegistryStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch 233 // a Rancher Environment. 234 func RegistryStateRefreshFunc(client *rancherClient.RancherClient, registryID string) resource.StateRefreshFunc { 235 return func() (interface{}, string, error) { 236 env, err := client.Registry.ById(registryID) 237 238 if err != nil { 239 return nil, "", err 240 } 241 242 return env, env.State, nil 243 } 244 }