github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_image_list_entry.go (about)

     1  package opc
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/go-oracle-terraform/compute"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/hashicorp/terraform/helper/structure"
    11  	"github.com/hashicorp/terraform/helper/validation"
    12  )
    13  
    14  func resourceOPCImageListEntry() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceOPCImageListEntryCreate,
    17  		Read:   resourceOPCImageListEntryRead,
    18  		Delete: resourceOPCImageListEntryDelete,
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": {
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  			"machine_images": {
    30  				Type:     schema.TypeList,
    31  				Required: true,
    32  				ForceNew: true,
    33  				Elem:     &schema.Schema{Type: schema.TypeString},
    34  			},
    35  			"version": {
    36  				Type:     schema.TypeInt,
    37  				ForceNew: true,
    38  				Required: true,
    39  			},
    40  			"attributes": {
    41  				Type:             schema.TypeString,
    42  				ForceNew:         true,
    43  				Optional:         true,
    44  				ValidateFunc:     validation.ValidateJsonString,
    45  				DiffSuppressFunc: structure.SuppressJsonDiff,
    46  			},
    47  			"uri": {
    48  				Type:     schema.TypeString,
    49  				Computed: true,
    50  			},
    51  		},
    52  	}
    53  }
    54  
    55  func resourceOPCImageListEntryCreate(d *schema.ResourceData, meta interface{}) error {
    56  	client := meta.(*compute.Client).ImageListEntries()
    57  
    58  	name := d.Get("name").(string)
    59  	machineImages := expandOPCImageListEntryMachineImages(d)
    60  	version := d.Get("version").(int)
    61  
    62  	createInput := &compute.CreateImageListEntryInput{
    63  		Name:          name,
    64  		MachineImages: machineImages,
    65  		Version:       version,
    66  	}
    67  
    68  	if v, ok := d.GetOk("attributes"); ok {
    69  		attributesString := v.(string)
    70  		attributes, err := structure.ExpandJsonFromString(attributesString)
    71  		if err != nil {
    72  			return err
    73  		}
    74  
    75  		createInput.Attributes = attributes
    76  	}
    77  
    78  	_, err := client.CreateImageListEntry(createInput)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	id := generateOPCImageListEntryID(name, version)
    84  	d.SetId(id)
    85  	return resourceOPCImageListEntryRead(d, meta)
    86  }
    87  
    88  func resourceOPCImageListEntryRead(d *schema.ResourceData, meta interface{}) error {
    89  	client := meta.(*compute.Client).ImageListEntries()
    90  
    91  	name, version, err := parseOPCImageListEntryID(d.Id())
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	getInput := compute.GetImageListEntryInput{
    97  		Name:    *name,
    98  		Version: *version,
    99  	}
   100  	getResult, err := client.GetImageListEntry(&getInput)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	attrs, err := structure.FlattenJsonToString(getResult.Attributes)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	d.Set("name", name)
   111  	d.Set("machine_images", getResult.MachineImages)
   112  	d.Set("version", getResult.Version)
   113  	d.Set("attributes", attrs)
   114  	d.Set("uri", getResult.Uri)
   115  
   116  	return nil
   117  }
   118  
   119  func resourceOPCImageListEntryDelete(d *schema.ResourceData, meta interface{}) error {
   120  	client := meta.(*compute.Client).ImageListEntries()
   121  
   122  	name, version, err := parseOPCImageListEntryID(d.Id())
   123  	if err != nil {
   124  		return err
   125  	}
   126  
   127  	deleteInput := &compute.DeleteImageListEntryInput{
   128  		Name:    *name,
   129  		Version: *version,
   130  	}
   131  	err = client.DeleteImageListEntry(deleteInput)
   132  	if err != nil {
   133  		return err
   134  	}
   135  
   136  	return nil
   137  }
   138  
   139  func parseOPCImageListEntryID(id string) (*string, *int, error) {
   140  	s := strings.Split(id, "|")
   141  	name, versionString := s[0], s[1]
   142  	version, err := strconv.Atoi(versionString)
   143  	if err != nil {
   144  		return nil, nil, err
   145  	}
   146  
   147  	return &name, &version, nil
   148  }
   149  
   150  func expandOPCImageListEntryMachineImages(d *schema.ResourceData) []string {
   151  	machineImages := []string{}
   152  	for _, i := range d.Get("machine_images").([]interface{}) {
   153  		machineImages = append(machineImages, i.(string))
   154  	}
   155  	return machineImages
   156  }
   157  
   158  func generateOPCImageListEntryID(name string, version int) string {
   159  	return fmt.Sprintf("%s|%d", name, version)
   160  }