github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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: schema.ImportStatePassthrough,
    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(&registry)
    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 := meta.(*Config)
    94  
    95  	registry, err := client.Registry.ById(d.Id())
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	log.Printf("[INFO] Registry Name: %s", registry.Name)
   101  
   102  	d.Set("description", registry.Description)
   103  	d.Set("name", registry.Name)
   104  	d.Set("server_address", registry.ServerAddress)
   105  	d.Set("environment_id", registry.AccountId)
   106  
   107  	return nil
   108  }
   109  
   110  func resourceRancherRegistryUpdate(d *schema.ResourceData, meta interface{}) error {
   111  	client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	registry, err := client.Registry.ById(d.Id())
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	name := d.Get("name").(string)
   122  	description := d.Get("description").(string)
   123  
   124  	registry.Name = name
   125  	registry.Description = description
   126  	client.Registry.Update(registry, &registry)
   127  
   128  	return resourceRancherRegistryRead(d, meta)
   129  }
   130  
   131  func resourceRancherRegistryDelete(d *schema.ResourceData, meta interface{}) error {
   132  	log.Printf("[INFO] Deleting Registry: %s", d.Id())
   133  	id := d.Id()
   134  	client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	reg, err := client.Registry.ById(id)
   140  	if err != nil {
   141  		return err
   142  	}
   143  
   144  	// Step 1: Deactivate
   145  	if _, e := client.Registry.ActionDeactivate(reg); e != nil {
   146  		return fmt.Errorf("Error deactivating Registry: %s", err)
   147  	}
   148  
   149  	log.Printf("[DEBUG] Waiting for registry (%s) to be deactivated", id)
   150  
   151  	stateConf := &resource.StateChangeConf{
   152  		Pending:    []string{"active", "inactive", "deactivating"},
   153  		Target:     []string{"inactive"},
   154  		Refresh:    RegistryStateRefreshFunc(client, id),
   155  		Timeout:    10 * time.Minute,
   156  		Delay:      1 * time.Second,
   157  		MinTimeout: 3 * time.Second,
   158  	}
   159  
   160  	_, waitErr := stateConf.WaitForState()
   161  	if waitErr != nil {
   162  		return fmt.Errorf(
   163  			"Error waiting for registry (%s) to be deactivated: %s", id, waitErr)
   164  	}
   165  
   166  	// Update resource to reflect its state
   167  	reg, err = client.Registry.ById(id)
   168  	if err != nil {
   169  		return fmt.Errorf("Failed to refresh state of deactivated registry (%s): %s", id, err)
   170  	}
   171  
   172  	// Step 2: Remove
   173  	if _, err := client.Registry.ActionRemove(reg); err != nil {
   174  		return fmt.Errorf("Error removing Registry: %s", err)
   175  	}
   176  
   177  	log.Printf("[DEBUG] Waiting for registry (%s) to be removed", id)
   178  
   179  	stateConf = &resource.StateChangeConf{
   180  		Pending:    []string{"inactive", "removed", "removing"},
   181  		Target:     []string{"removed"},
   182  		Refresh:    RegistryStateRefreshFunc(client, id),
   183  		Timeout:    10 * time.Minute,
   184  		Delay:      1 * time.Second,
   185  		MinTimeout: 3 * time.Second,
   186  	}
   187  
   188  	_, waitErr = stateConf.WaitForState()
   189  	if waitErr != nil {
   190  		return fmt.Errorf(
   191  			"Error waiting for registry (%s) to be removed: %s", id, waitErr)
   192  	}
   193  
   194  	d.SetId("")
   195  	return nil
   196  }
   197  
   198  // RegistryStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
   199  // a Rancher Environment.
   200  func RegistryStateRefreshFunc(client *rancherClient.RancherClient, registryID string) resource.StateRefreshFunc {
   201  	return func() (interface{}, string, error) {
   202  		env, err := client.Registry.ById(registryID)
   203  
   204  		if err != nil {
   205  			return nil, "", err
   206  		}
   207  
   208  		return env, env.State, nil
   209  	}
   210  }