github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/rancher/resource_rancher_environment.go (about) 1 package rancher 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 "time" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/helper/schema" 11 "github.com/hashicorp/terraform/helper/validation" 12 rancherClient "github.com/rancher/go-rancher/client" 13 ) 14 15 func resourceRancherEnvironment() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceRancherEnvironmentCreate, 18 Read: resourceRancherEnvironmentRead, 19 Update: resourceRancherEnvironmentUpdate, 20 Delete: resourceRancherEnvironmentDelete, 21 Importer: &schema.ResourceImporter{ 22 State: schema.ImportStatePassthrough, 23 }, 24 25 Schema: map[string]*schema.Schema{ 26 "id": &schema.Schema{ 27 Type: schema.TypeString, 28 Computed: true, 29 }, 30 "name": &schema.Schema{ 31 Type: schema.TypeString, 32 Required: true, 33 }, 34 "orchestration": &schema.Schema{ 35 Type: schema.TypeString, 36 Default: "cattle", 37 Optional: true, 38 ValidateFunc: validation.StringInSlice([]string{"cattle", "kubernetes", "mesos", "swarm"}, true), 39 }, 40 "description": &schema.Schema{ 41 Type: schema.TypeString, 42 Optional: true, 43 }, 44 }, 45 } 46 } 47 48 func resourceRancherEnvironmentCreate(d *schema.ResourceData, meta interface{}) error { 49 log.Printf("[INFO] Creating Environment: %s", d.Id()) 50 client := meta.(*Config) 51 52 name := d.Get("name").(string) 53 description := d.Get("description").(string) 54 orchestration := d.Get("orchestration").(string) 55 56 data := map[string]interface{}{ 57 "name": &name, 58 "description": &description, 59 } 60 61 setOrchestrationFields(orchestration, data) 62 63 var newEnv rancherClient.Project 64 if err := client.Create("project", data, &newEnv); err != nil { 65 return err 66 } 67 68 stateConf := &resource.StateChangeConf{ 69 Pending: []string{"active", "removed", "removing"}, 70 Target: []string{"active"}, 71 Refresh: EnvironmentStateRefreshFunc(client, newEnv.Id), 72 Timeout: 10 * time.Minute, 73 Delay: 1 * time.Second, 74 MinTimeout: 3 * time.Second, 75 } 76 _, waitErr := stateConf.WaitForState() 77 if waitErr != nil { 78 return fmt.Errorf( 79 "Error waiting for environment (%s) to be created: %s", newEnv.Id, waitErr) 80 } 81 82 d.SetId(newEnv.Id) 83 log.Printf("[INFO] Environment ID: %s", d.Id()) 84 85 return resourceRancherEnvironmentRead(d, meta) 86 } 87 88 func resourceRancherEnvironmentRead(d *schema.ResourceData, meta interface{}) error { 89 log.Printf("[INFO] Refreshing Environment: %s", d.Id()) 90 client := meta.(*Config) 91 92 env, err := client.Project.ById(d.Id()) 93 if err != nil { 94 return err 95 } 96 97 log.Printf("[INFO] Environment Name: %s", env.Name) 98 99 d.Set("description", env.Description) 100 d.Set("name", env.Name) 101 d.Set("orchestration", GetActiveOrchestration(env)) 102 103 return nil 104 } 105 106 func resourceRancherEnvironmentUpdate(d *schema.ResourceData, meta interface{}) error { 107 client := meta.(*Config) 108 109 name := d.Get("name").(string) 110 description := d.Get("description").(string) 111 orchestration := d.Get("orchestration").(string) 112 113 data := map[string]interface{}{ 114 "name": &name, 115 "description": &description, 116 } 117 118 setOrchestrationFields(orchestration, data) 119 120 var newEnv rancherClient.Project 121 env, err := client.Project.ById(d.Id()) 122 if err != nil { 123 return err 124 } 125 126 if err := client.Update("project", &env.Resource, data, &newEnv); err != nil { 127 return err 128 } 129 130 return resourceRancherEnvironmentRead(d, meta) 131 } 132 133 func resourceRancherEnvironmentDelete(d *schema.ResourceData, meta interface{}) error { 134 log.Printf("[INFO] Deleting Environment: %s", d.Id()) 135 id := d.Id() 136 client := meta.(*Config) 137 138 env, err := client.Project.ById(id) 139 if err != nil { 140 return err 141 } 142 143 if err := client.Project.Delete(env); err != nil { 144 return fmt.Errorf("Error deleting Environment: %s", err) 145 } 146 147 log.Printf("[DEBUG] Waiting for environment (%s) to be removed", id) 148 149 stateConf := &resource.StateChangeConf{ 150 Pending: []string{"active", "removed", "removing"}, 151 Target: []string{"removed"}, 152 Refresh: EnvironmentStateRefreshFunc(client, id), 153 Timeout: 10 * time.Minute, 154 Delay: 1 * time.Second, 155 MinTimeout: 3 * time.Second, 156 } 157 158 _, waitErr := stateConf.WaitForState() 159 if waitErr != nil { 160 return fmt.Errorf( 161 "Error waiting for environment (%s) to be removed: %s", id, waitErr) 162 } 163 164 d.SetId("") 165 return nil 166 } 167 168 func setOrchestrationFields(orchestration string, data map[string]interface{}) { 169 orch := strings.ToLower(orchestration) 170 171 data["swarm"] = false 172 data["kubernetes"] = false 173 data["mesos"] = false 174 175 if orch == "k8s" { 176 orch = "kubernetes" 177 } 178 179 data[orch] = true 180 } 181 182 // EnvironmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch 183 // a Rancher Environment. 184 func EnvironmentStateRefreshFunc(client *Config, environmentID string) resource.StateRefreshFunc { 185 return func() (interface{}, string, error) { 186 env, err := client.Project.ById(environmentID) 187 188 if err != nil { 189 return nil, "", err 190 } 191 192 return env, env.State, nil 193 } 194 }