github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/rancher/resource_rancher_registration_token.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 resourceRancherRegistrationToken() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceRancherRegistrationTokenCreate,
    16  		Read:   resourceRancherRegistrationTokenRead,
    17  		Delete: resourceRancherRegistrationTokenDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"id": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Computed: true,
    26  			},
    27  			"name": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  			"description": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  				ForceNew: true,
    36  			},
    37  			"environment_id": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  			"token": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Computed: true,
    45  			},
    46  			"registration_url": &schema.Schema{
    47  				Type:     schema.TypeString,
    48  				Computed: true,
    49  			},
    50  			"command": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Computed: true,
    53  			},
    54  		},
    55  	}
    56  }
    57  
    58  func resourceRancherRegistrationTokenCreate(d *schema.ResourceData, meta interface{}) error {
    59  	log.Printf("[INFO] Creating RegistrationToken: %s", d.Id())
    60  	client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	name := d.Get("name").(string)
    66  	description := d.Get("description").(string)
    67  
    68  	data := map[string]interface{}{
    69  		"name":        &name,
    70  		"description": &description,
    71  	}
    72  
    73  	var newRegT rancherClient.RegistrationToken
    74  	if err := client.Create("registrationToken", data, &newRegT); err != nil {
    75  		return err
    76  	}
    77  
    78  	stateConf := &resource.StateChangeConf{
    79  		Pending:    []string{"active", "removed", "removing"},
    80  		Target:     []string{"active"},
    81  		Refresh:    RegistrationTokenStateRefreshFunc(client, newRegT.Id),
    82  		Timeout:    10 * time.Minute,
    83  		Delay:      1 * time.Second,
    84  		MinTimeout: 3 * time.Second,
    85  	}
    86  	_, waitErr := stateConf.WaitForState()
    87  	if waitErr != nil {
    88  		return fmt.Errorf(
    89  			"Error waiting for registration token (%s) to be created: %s", newRegT.Id, waitErr)
    90  	}
    91  
    92  	d.SetId(newRegT.Id)
    93  	log.Printf("[INFO] RegistrationToken ID: %s", d.Id())
    94  
    95  	return resourceRancherRegistrationTokenRead(d, meta)
    96  }
    97  
    98  func resourceRancherRegistrationTokenRead(d *schema.ResourceData, meta interface{}) error {
    99  	log.Printf("[INFO] Refreshing RegistrationToken: %s", d.Id())
   100  	client := meta.(*Config)
   101  
   102  	regT, err := client.RegistrationToken.ById(d.Id())
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	log.Printf("[INFO] RegistrationToken Name: %s", regT.Name)
   108  
   109  	d.Set("description", regT.Description)
   110  	d.Set("name", regT.Name)
   111  	d.Set("token", regT.Token)
   112  	d.Set("registration_url", regT.RegistrationUrl)
   113  	d.Set("environment_id", regT.AccountId)
   114  	d.Set("command", regT.Command)
   115  
   116  	return nil
   117  }
   118  
   119  func resourceRancherRegistrationTokenDelete(d *schema.ResourceData, meta interface{}) error {
   120  	log.Printf("[INFO] Deleting RegistrationToken: %s", d.Id())
   121  	id := d.Id()
   122  	client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
   123  	if err != nil {
   124  		return err
   125  	}
   126  
   127  	regT, err := client.RegistrationToken.ById(id)
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	// Step 1: Deactivate
   133  	if _, e := client.RegistrationToken.ActionDeactivate(regT); e != nil {
   134  		return fmt.Errorf("Error deactivating RegistrationToken: %s", err)
   135  	}
   136  
   137  	log.Printf("[DEBUG] Waiting for registration token (%s) to be deactivated", id)
   138  
   139  	stateConf := &resource.StateChangeConf{
   140  		Pending:    []string{"active", "inactive", "deactivating"},
   141  		Target:     []string{"inactive"},
   142  		Refresh:    RegistrationTokenStateRefreshFunc(client, id),
   143  		Timeout:    10 * time.Minute,
   144  		Delay:      1 * time.Second,
   145  		MinTimeout: 3 * time.Second,
   146  	}
   147  
   148  	_, waitErr := stateConf.WaitForState()
   149  	if waitErr != nil {
   150  		return fmt.Errorf(
   151  			"Error waiting for registration token (%s) to be deactivated: %s", id, waitErr)
   152  	}
   153  
   154  	// Update resource to reflect its state
   155  	regT, err = client.RegistrationToken.ById(id)
   156  	if err != nil {
   157  		return fmt.Errorf("Failed to refresh state of deactivated registration token (%s): %s", id, err)
   158  	}
   159  
   160  	// Step 2: Remove
   161  	if _, err := client.RegistrationToken.ActionRemove(regT); err != nil {
   162  		return fmt.Errorf("Error removing RegistrationToken: %s", err)
   163  	}
   164  
   165  	log.Printf("[DEBUG] Waiting for registration token (%s) to be removed", id)
   166  
   167  	stateConf = &resource.StateChangeConf{
   168  		Pending:    []string{"inactive", "removed", "removing"},
   169  		Target:     []string{"removed"},
   170  		Refresh:    RegistrationTokenStateRefreshFunc(client, id),
   171  		Timeout:    10 * time.Minute,
   172  		Delay:      1 * time.Second,
   173  		MinTimeout: 3 * time.Second,
   174  	}
   175  
   176  	_, waitErr = stateConf.WaitForState()
   177  	if waitErr != nil {
   178  		return fmt.Errorf(
   179  			"Error waiting for registration token (%s) to be removed: %s", id, waitErr)
   180  	}
   181  
   182  	d.SetId("")
   183  	return nil
   184  }
   185  
   186  // RegistrationTokenStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
   187  // a Rancher RegistrationToken.
   188  func RegistrationTokenStateRefreshFunc(client *rancherClient.RancherClient, regTID string) resource.StateRefreshFunc {
   189  	return func() (interface{}, string, error) {
   190  		regT, err := client.RegistrationToken.ById(regTID)
   191  
   192  		if err != nil {
   193  			return nil, "", err
   194  		}
   195  
   196  		return regT, regT.State, nil
   197  	}
   198  }