github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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: resourceRancherRegistryCredentialImport,
    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(&registryCred)
    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, err := meta.(*Config).RegistryClient(d.Get("registry_id").(string))
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	registryCred, err := client.RegistryCredential.ById(d.Id())
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	if registryCred == nil {
   118  		log.Printf("[INFO] RegistryCredential %s not found", d.Id())
   119  		d.SetId("")
   120  		return nil
   121  	}
   122  
   123  	if removed(registryCred.State) {
   124  		log.Printf("[INFO] Registry Credential %s was removed on %v", d.Id(), registryCred.Removed)
   125  		d.SetId("")
   126  		return nil
   127  	}
   128  
   129  	log.Printf("[INFO] RegistryCredential Name: %s", registryCred.Name)
   130  
   131  	d.Set("description", registryCred.Description)
   132  	d.Set("name", registryCred.Name)
   133  	d.Set("email", registryCred.Email)
   134  	d.Set("public_value", registryCred.PublicValue)
   135  	d.Set("registry_id", registryCred.RegistryId)
   136  
   137  	return nil
   138  }
   139  
   140  func resourceRancherRegistryCredentialUpdate(d *schema.ResourceData, meta interface{}) error {
   141  	log.Printf("[INFO] Updating RegistryCredential: %s", d.Id())
   142  	client, err := meta.(*Config).RegistryClient(d.Get("registry_id").(string))
   143  	if err != nil {
   144  		return err
   145  	}
   146  
   147  	registryCred, err := client.RegistryCredential.ById(d.Id())
   148  	if err != nil {
   149  		return err
   150  	}
   151  
   152  	name := d.Get("name").(string)
   153  	description := d.Get("description").(string)
   154  	email := d.Get("email").(string)
   155  	publicValue := d.Get("public_value").(string)
   156  	secretValue := d.Get("secret_value").(string)
   157  
   158  	registryCred.Name = name
   159  	registryCred.Description = description
   160  	registryCred.Email = email
   161  	registryCred.PublicValue = publicValue
   162  	registryCred.SecretValue = secretValue
   163  	client.RegistryCredential.Update(registryCred, &registryCred)
   164  
   165  	return resourceRancherRegistryCredentialRead(d, meta)
   166  }
   167  
   168  func resourceRancherRegistryCredentialDelete(d *schema.ResourceData, meta interface{}) error {
   169  	log.Printf("[INFO] Deleting RegistryCredential: %s", d.Id())
   170  	id := d.Id()
   171  	client, err := meta.(*Config).RegistryClient(d.Get("registry_id").(string))
   172  	if err != nil {
   173  		return err
   174  	}
   175  
   176  	reg, err := client.RegistryCredential.ById(id)
   177  	if err != nil {
   178  		return err
   179  	}
   180  
   181  	// Step 1: Deactivate
   182  	if _, e := client.RegistryCredential.ActionDeactivate(reg); e != nil {
   183  		return fmt.Errorf("Error deactivating RegistryCredential: %s", err)
   184  	}
   185  
   186  	log.Printf("[DEBUG] Waiting for registry credential (%s) to be deactivated", id)
   187  
   188  	stateConf := &resource.StateChangeConf{
   189  		Pending:    []string{"active", "inactive", "deactivating"},
   190  		Target:     []string{"inactive"},
   191  		Refresh:    RegistryCredentialStateRefreshFunc(client, id),
   192  		Timeout:    10 * time.Minute,
   193  		Delay:      1 * time.Second,
   194  		MinTimeout: 3 * time.Second,
   195  	}
   196  
   197  	_, waitErr := stateConf.WaitForState()
   198  	if waitErr != nil {
   199  		return fmt.Errorf(
   200  			"Error waiting for registry credential (%s) to be deactivated: %s", id, waitErr)
   201  	}
   202  
   203  	// Update resource to reflect its state
   204  	reg, err = client.RegistryCredential.ById(id)
   205  	if err != nil {
   206  		return fmt.Errorf("Failed to refresh state of deactivated registry credential (%s): %s", id, err)
   207  	}
   208  
   209  	// Step 2: Remove
   210  	if _, err := client.RegistryCredential.ActionRemove(reg); err != nil {
   211  		return fmt.Errorf("Error removing RegistryCredential: %s", err)
   212  	}
   213  
   214  	log.Printf("[DEBUG] Waiting for registry (%s) to be removed", id)
   215  
   216  	stateConf = &resource.StateChangeConf{
   217  		Pending:    []string{"inactive", "removed", "removing"},
   218  		Target:     []string{"removed"},
   219  		Refresh:    RegistryCredentialStateRefreshFunc(client, id),
   220  		Timeout:    10 * time.Minute,
   221  		Delay:      1 * time.Second,
   222  		MinTimeout: 3 * time.Second,
   223  	}
   224  
   225  	_, waitErr = stateConf.WaitForState()
   226  	if waitErr != nil {
   227  		return fmt.Errorf(
   228  			"Error waiting for registry (%s) to be removed: %s", id, waitErr)
   229  	}
   230  
   231  	d.SetId("")
   232  	return nil
   233  }
   234  
   235  func resourceRancherRegistryCredentialImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
   236  	regID, resourceID := splitID(d.Id())
   237  	d.SetId(resourceID)
   238  	if regID != "" {
   239  		d.Set("registry_id", regID)
   240  	} else {
   241  		client, err := meta.(*Config).GlobalClient()
   242  		if err != nil {
   243  			return []*schema.ResourceData{}, err
   244  		}
   245  		cred, err := client.RegistryCredential.ById(d.Id())
   246  		if err != nil {
   247  			return []*schema.ResourceData{}, err
   248  		}
   249  		d.Set("registry_id", cred.RegistryId)
   250  	}
   251  	return []*schema.ResourceData{d}, nil
   252  }
   253  
   254  // RegistryCredentialStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
   255  // a Rancher Environment.
   256  func RegistryCredentialStateRefreshFunc(client *rancherClient.RancherClient, registryCredID string) resource.StateRefreshFunc {
   257  	return func() (interface{}, string, error) {
   258  		regC, err := client.RegistryCredential.ById(registryCredID)
   259  
   260  		if err != nil {
   261  			return nil, "", err
   262  		}
   263  
   264  		return regC, regC.State, nil
   265  	}
   266  }