github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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: resourceRancherRegistrationTokenImport,
    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  			"image": &schema.Schema{
    55  				Type:     schema.TypeString,
    56  				Computed: true,
    57  			},
    58  		},
    59  	}
    60  }
    61  
    62  func resourceRancherRegistrationTokenCreate(d *schema.ResourceData, meta interface{}) error {
    63  	log.Printf("[INFO] Creating RegistrationToken: %s", d.Id())
    64  	client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	name := d.Get("name").(string)
    70  	description := d.Get("description").(string)
    71  
    72  	data := map[string]interface{}{
    73  		"name":        &name,
    74  		"description": &description,
    75  	}
    76  
    77  	var newRegT rancherClient.RegistrationToken
    78  	if err := client.Create("registrationToken", data, &newRegT); err != nil {
    79  		return err
    80  	}
    81  
    82  	stateConf := &resource.StateChangeConf{
    83  		Pending:    []string{"active", "removed", "removing"},
    84  		Target:     []string{"active"},
    85  		Refresh:    RegistrationTokenStateRefreshFunc(client, newRegT.Id),
    86  		Timeout:    10 * time.Minute,
    87  		Delay:      1 * time.Second,
    88  		MinTimeout: 3 * time.Second,
    89  	}
    90  	_, waitErr := stateConf.WaitForState()
    91  	if waitErr != nil {
    92  		return fmt.Errorf(
    93  			"Error waiting for registration token (%s) to be created: %s", newRegT.Id, waitErr)
    94  	}
    95  
    96  	d.SetId(newRegT.Id)
    97  	log.Printf("[INFO] RegistrationToken ID: %s", d.Id())
    98  
    99  	return resourceRancherRegistrationTokenRead(d, meta)
   100  }
   101  
   102  func resourceRancherRegistrationTokenRead(d *schema.ResourceData, meta interface{}) error {
   103  	log.Printf("[INFO] Refreshing RegistrationToken: %s", d.Id())
   104  	client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
   105  	if err != nil {
   106  		return err
   107  	}
   108  	// client := meta.(*Config)
   109  
   110  	regT, err := client.RegistrationToken.ById(d.Id())
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	if regT == nil {
   116  		log.Printf("[INFO] RegistrationToken %s not found", d.Id())
   117  		d.SetId("")
   118  		return nil
   119  	}
   120  
   121  	if removed(regT.State) {
   122  		log.Printf("[INFO] Registration Token %s was removed on %v", d.Id(), regT.Removed)
   123  		d.SetId("")
   124  		return nil
   125  	}
   126  
   127  	log.Printf("[INFO] RegistrationToken Name: %s", regT.Name)
   128  
   129  	d.Set("description", regT.Description)
   130  	d.Set("name", regT.Name)
   131  	d.Set("token", regT.Token)
   132  	d.Set("registration_url", regT.RegistrationUrl)
   133  	d.Set("environment_id", regT.AccountId)
   134  	d.Set("command", regT.Command)
   135  	d.Set("image", regT.Image)
   136  
   137  	return nil
   138  }
   139  
   140  func resourceRancherRegistrationTokenDelete(d *schema.ResourceData, meta interface{}) error {
   141  	log.Printf("[INFO] Deleting RegistrationToken: %s", d.Id())
   142  	id := d.Id()
   143  	client, err := meta.(*Config).EnvironmentClient(d.Get("environment_id").(string))
   144  	if err != nil {
   145  		return err
   146  	}
   147  
   148  	regT, err := client.RegistrationToken.ById(id)
   149  	if err != nil {
   150  		return err
   151  	}
   152  
   153  	// Step 1: Deactivate
   154  	if _, e := client.RegistrationToken.ActionDeactivate(regT); e != nil {
   155  		return fmt.Errorf("Error deactivating RegistrationToken: %s", err)
   156  	}
   157  
   158  	log.Printf("[DEBUG] Waiting for registration token (%s) to be deactivated", id)
   159  
   160  	stateConf := &resource.StateChangeConf{
   161  		Pending:    []string{"active", "inactive", "deactivating"},
   162  		Target:     []string{"inactive"},
   163  		Refresh:    RegistrationTokenStateRefreshFunc(client, id),
   164  		Timeout:    10 * time.Minute,
   165  		Delay:      1 * time.Second,
   166  		MinTimeout: 3 * time.Second,
   167  	}
   168  
   169  	_, waitErr := stateConf.WaitForState()
   170  	if waitErr != nil {
   171  		return fmt.Errorf(
   172  			"Error waiting for registration token (%s) to be deactivated: %s", id, waitErr)
   173  	}
   174  
   175  	// Update resource to reflect its state
   176  	regT, err = client.RegistrationToken.ById(id)
   177  	if err != nil {
   178  		return fmt.Errorf("Failed to refresh state of deactivated registration token (%s): %s", id, err)
   179  	}
   180  
   181  	// Step 2: Remove
   182  	if _, err := client.RegistrationToken.ActionRemove(regT); err != nil {
   183  		return fmt.Errorf("Error removing RegistrationToken: %s", err)
   184  	}
   185  
   186  	log.Printf("[DEBUG] Waiting for registration token (%s) to be removed", id)
   187  
   188  	stateConf = &resource.StateChangeConf{
   189  		Pending:    []string{"inactive", "removed", "removing"},
   190  		Target:     []string{"removed"},
   191  		Refresh:    RegistrationTokenStateRefreshFunc(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 registration token (%s) to be removed: %s", id, waitErr)
   201  	}
   202  
   203  	d.SetId("")
   204  	return nil
   205  }
   206  
   207  func resourceRancherRegistrationTokenImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
   208  	envID, resourceID := splitID(d.Id())
   209  	d.SetId(resourceID)
   210  	if envID != "" {
   211  		d.Set("environment_id", envID)
   212  	} else {
   213  		client, err := meta.(*Config).GlobalClient()
   214  		if err != nil {
   215  			return []*schema.ResourceData{}, err
   216  		}
   217  		token, err := client.RegistrationToken.ById(d.Id())
   218  		if err != nil {
   219  			return []*schema.ResourceData{}, err
   220  		}
   221  		d.Set("environment_id", token.AccountId)
   222  	}
   223  	return []*schema.ResourceData{d}, nil
   224  }
   225  
   226  // RegistrationTokenStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
   227  // a Rancher RegistrationToken.
   228  func RegistrationTokenStateRefreshFunc(client *rancherClient.RancherClient, regTID string) resource.StateRefreshFunc {
   229  	return func() (interface{}, string, error) {
   230  		regT, err := client.RegistrationToken.ById(regTID)
   231  
   232  		if err != nil {
   233  			return nil, "", err
   234  		}
   235  
   236  		return regT, regT.State, nil
   237  	}
   238  }