Skip to content

Commit

Permalink
add instance flavors and disk size
Browse files Browse the repository at this point in the history
  • Loading branch information
rmb938 committed Jan 14, 2018
1 parent 9cdb7c9 commit 1d70047
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 12 deletions.
21 changes: 11 additions & 10 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import:
- package: github.com/hashicorp/terraform
version: 0.10.0
- package: github.com/sandwichcloud/deli-cli
version: 0.0.10
version: 0.0.14
1 change: 1 addition & 0 deletions sandwich/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Provider() *schema.Provider {
"sandwich_project": resourceProject(),
"sandwich_image": resourceImage(),
"sandwich_keypair": resourceKeypair(),
"sandwich_flavor": resourceFlavor(),
"sandwich_instance": resourceInstance(),
},
ConfigureFunc: configureProvider,
Expand Down
91 changes: 91 additions & 0 deletions sandwich/resource_sandwich_flavor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package sandwich

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/sandwichcloud/deli-cli/api"
)

func resourceFlavor() *schema.Resource {
return &schema.Resource{
Create: resourceFlavorCreate,
Read: resourceFlavorRead,
Delete: resourceFlavorDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vcpus": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"ram": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"disk": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
},
}
}

func resourceFlavorCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
flavorClient := config.SandwichClient.Flavor()
name := d.Get("name").(string)
vcpus := d.Get("vcpus").(int)
ram := d.Get("ram").(int)
disk := d.Get("disk").(int)

flavor, err := flavorClient.Create(name, vcpus, ram, disk)
if err != nil {
return err
}

d.SetId(flavor.ID.String())

return resourceFlavorRead(d, meta)
}

func resourceFlavorRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
flavorClient := config.SandwichClient.Flavor()

flavor, err := flavorClient.Get(d.Id())
if err != nil {
if apiError, ok := err.(api.APIErrorInterface); ok {
if apiError.IsNotFound() {
d.SetId("")
return nil
}
}
return err
}

d.Set("name", flavor.Name)
d.Set("vcpus", flavor.VCPUS)
d.Set("ram", flavor.Ram)
d.Set("disk", flavor.Disk)

return nil
}

func resourceFlavorDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
flavorClient := config.SandwichClient.Flavor()

err := flavorClient.Delete(d.Id())
if err != nil {
return err
}

d.SetId("")
return nil
}
17 changes: 16 additions & 1 deletion sandwich/resource_sandwich_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func resourceInstance() *schema.Resource {
Optional: true,
Computed: true,
},
"flavor_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"disk": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"keypair_ids": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -83,6 +94,8 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
networkID := d.Get("network_id").(string)
regionID := d.Get("region_id").(string)
zoneID := d.Get("zone_id").(string)
flavorID := d.Get("flavor_id").(string)
disk := d.Get("disk").(int)
var keypairIDs []string
tags := map[string]string{}

Expand All @@ -94,7 +107,7 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
tags[k] = v.(string)
}

instance, err := instanceClient.Create(name, imageID, regionID, zoneID, networkID, serviceAccountID, keypairIDs, tags)
instance, err := instanceClient.Create(name, imageID, regionID, zoneID, networkID, serviceAccountID, flavorID, disk, keypairIDs, tags)
if err != nil {
return err
}
Expand Down Expand Up @@ -147,6 +160,8 @@ func resourceInstanceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("network_id", networkPort.NetworkID.String())
d.Set("region_id", instance.RegionID.String())
d.Set("zone_id", instance.ZoneID.String())
d.Set("flavor_id", instance.FlavorID.String())
d.Set("disk", instance.Disk)
var keypairIDs []string
tags := map[string]string{}

Expand Down

0 comments on commit 1d70047

Please sign in to comment.