github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/clc/resource_clc_group.go (about)

     1  package clc
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/CenturyLinkCloud/clc-sdk"
     9  	"github.com/CenturyLinkCloud/clc-sdk/api"
    10  	"github.com/CenturyLinkCloud/clc-sdk/group"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceCLCGroup() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceCLCGroupCreate,
    18  		Read:   resourceCLCGroupRead,
    19  		Update: resourceCLCGroupUpdate,
    20  		Delete: resourceCLCGroupDelete,
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  			},
    26  			"description": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  				Default:  "",
    30  			},
    31  			"parent": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  			},
    35  			"location_id": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  			},
    39  			"parent_group_id": &schema.Schema{
    40  				Type:     schema.TypeString,
    41  				Computed: true,
    42  			},
    43  			"custom_fields": &schema.Schema{
    44  				Type:     schema.TypeList,
    45  				Optional: true,
    46  				Elem:     &schema.Schema{Type: schema.TypeMap},
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func resourceCLCGroupCreate(d *schema.ResourceData, meta interface{}) error {
    53  	client := meta.(*clc.Client)
    54  	name := d.Get("name").(string)
    55  	desc := d.Get("description").(string)
    56  	parent := d.Get("parent").(string)
    57  	dc := d.Get("location_id").(string)
    58  
    59  	// clc doesn't enforce uniqueness by name
    60  	// so skip the trad'l error we'd raise
    61  	e, err := resolveGroupByNameOrId(name, dc, client)
    62  	if e != "" {
    63  		log.Printf("[INFO] Resolved existing group: %v => %v", name, e)
    64  		d.SetId(e)
    65  		return nil
    66  	}
    67  
    68  	var pgid string
    69  	p, err := resolveGroupByNameOrId(parent, dc, client)
    70  	if p != "" {
    71  		log.Printf("[INFO] Resolved parent group: %v => %v", parent, p)
    72  		pgid = p
    73  	} else {
    74  		return fmt.Errorf("Failed resolving parent group %s - %s err:%s", parent, p, err)
    75  	}
    76  
    77  	d.Set("parent_group_id", pgid)
    78  	spec := group.Group{
    79  		Name:          name,
    80  		Description:   desc,
    81  		ParentGroupID: pgid,
    82  	}
    83  	resp, err := client.Group.Create(spec)
    84  	if err != nil {
    85  		return fmt.Errorf("Failed creating group: %s", err)
    86  	}
    87  	log.Println("[INFO] Group created")
    88  	d.SetId(resp.ID)
    89  	return nil
    90  }
    91  
    92  func resourceCLCGroupRead(d *schema.ResourceData, meta interface{}) error {
    93  	client := meta.(*clc.Client)
    94  	id := d.Id()
    95  	g, err := client.Group.Get(id)
    96  	if err != nil {
    97  		log.Printf("[INFO] Failed finding group: %s -  %s. Marking destroyed", id, err)
    98  		d.SetId("")
    99  		return nil
   100  	}
   101  	d.Set("name", g.Name)
   102  	d.Set("description", g.Description)
   103  	d.Set("parent_group_id", g.ParentGroupID())
   104  	return nil
   105  }
   106  
   107  func resourceCLCGroupUpdate(d *schema.ResourceData, meta interface{}) error {
   108  	client := meta.(*clc.Client)
   109  	id := d.Id()
   110  	var err error
   111  	var patches []api.Update
   112  
   113  	g, err := client.Group.Get(id)
   114  	if err != nil {
   115  		return fmt.Errorf("Failed fetching group: %v - %v", id, err)
   116  	}
   117  
   118  	if delta, orig := d.Get("name").(string), g.Name; delta != orig {
   119  		patches = append(patches, group.UpdateName(delta))
   120  	}
   121  	if delta, orig := d.Get("description").(string), g.Description; delta != orig {
   122  		patches = append(patches, group.UpdateDescription(delta))
   123  	}
   124  	newParent := d.Get("parent").(string)
   125  	pgid, err := resolveGroupByNameOrId(newParent, g.Locationid, client)
   126  	log.Printf("[DEBUG] PARENT current:%v new:%v resolved:%v", g.ParentGroupID(), newParent, pgid)
   127  	if pgid == "" {
   128  		return fmt.Errorf("Unable to resolve parent group %v: %v", newParent, err)
   129  	} else if newParent != g.ParentGroupID() {
   130  		patches = append(patches, group.UpdateParentGroupID(pgid))
   131  	}
   132  
   133  	if len(patches) == 0 {
   134  		return nil
   135  	}
   136  	err = client.Group.Update(id, patches...)
   137  	if err != nil {
   138  		return fmt.Errorf("Failed updating group %v: %v", id, err)
   139  	}
   140  	return resource.Retry(1*time.Minute, func() *resource.RetryError {
   141  		_, err := client.Group.Get(id)
   142  		if err != nil {
   143  			return resource.RetryableError(err)
   144  		}
   145  		err = resourceCLCGroupRead(d, meta)
   146  		if err != nil {
   147  			return resource.NonRetryableError(err)
   148  		}
   149  		return nil
   150  	})
   151  }
   152  
   153  func resourceCLCGroupDelete(d *schema.ResourceData, meta interface{}) error {
   154  	client := meta.(*clc.Client)
   155  	id := d.Id()
   156  	log.Printf("[INFO] Deleting group %v", id)
   157  	st, err := client.Group.Delete(id)
   158  	if err != nil {
   159  		return fmt.Errorf("Failed deleting group: %v with err: %v", id, err)
   160  	}
   161  	waitStatus(client, st.ID)
   162  	return nil
   163  }