Skip to content

Commit

Permalink
add project members and quota
Browse files Browse the repository at this point in the history
  • Loading branch information
rmb938 committed Jan 24, 2018
1 parent ef144a4 commit 2ebdb9a
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 20 deletions.
19 changes: 10 additions & 9 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.17
version: 0.0.20
22 changes: 12 additions & 10 deletions sandwich/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ func Provider() *schema.Provider {
"sandwich_network": dataSourceNetwork(),
},
ResourcesMap: map[string]*schema.Resource{
"sandwich_region": resourceRegion(),
"sandwich_zone": resourceZone(),
"sandwich_network": resourceNetwork(),
"sandwich_project": resourceProject(),
"sandwich_image": resourceImage(),
"sandwich_image_member": resourceImageMember(),
"sandwich_keypair": resourceKeypair(),
"sandwich_flavor": resourceFlavor(),
"sandwich_instance": resourceInstance(),
"sandwich_volume": resourceVolume(),
"sandwich_region": resourceRegion(),
"sandwich_zone": resourceZone(),
"sandwich_network": resourceNetwork(),
"sandwich_project": resourceProject(),
"sandwich_project_quota": resourceProjectQuota(),
"sandwich_project_member": resourceProjectMember(),
"sandwich_image": resourceImage(),
"sandwich_image_member": resourceImageMember(),
"sandwich_keypair": resourceKeypair(),
"sandwich_flavor": resourceFlavor(),
"sandwich_instance": resourceInstance(),
"sandwich_volume": resourceVolume(),
},
ConfigureFunc: configureProvider,
}
Expand Down
110 changes: 110 additions & 0 deletions sandwich/resource_sandwich_project_member.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package sandwich

import (
"time"

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

func resourceProjectMember() *schema.Resource {
return &schema.Resource{
Create: resourceProjectMemberCreate,
Read: resourceProjectMemberRead,
Update: resourceProjectMemberUpdate,
Delete: resourceProjectmemberDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Read: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"driver": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"roles": {
Type: schema.TypeList,
Required: false,
Optional: true,
ForceNew: false,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func resourceProjectMemberCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
projectClient := config.SandwichClient.Project()

username := d.Get("username").(string)
driver := d.Get("driver").(string)

member, err := projectClient.AddMember(username, driver)
if err != nil {
return err
}

d.SetId(member.ID.String())

return resourceProjectQuotaUpdate(d, meta)
}

func resourceProjectMemberRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
projectClient := config.SandwichClient.Project()

member, err := projectClient.GetMember(d.Id())
if err != nil {
if apiError, ok := err.(api.APIErrorInterface); ok {
if apiError.IsNotFound() {
d.SetId("")
return nil
}
}
}

d.Set("username", member.Username)
d.Set("driver", member.Driver)
d.Set("roles", member.Roles)

return nil
}

func resourceProjectMemberUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
projectClient := config.SandwichClient.Project()

roles := d.Get("roles").([]string)

err := projectClient.UpdateMember(d.Id(), roles)
if err != nil {
return err
}

return resourceProjectQuotaRead(d, meta)
}

func resourceProjectmemberDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
projectClient := config.SandwichClient.Project()

err := projectClient.RemoveMember(d.Id())
if err != nil {
return err
}

return nil
}
86 changes: 86 additions & 0 deletions sandwich/resource_sandwich_project_quota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package sandwich

import (
"time"

"github.com/hashicorp/terraform/helper/schema"
"github.com/sandwichcloud/deli-cli/api"
"github.com/satori/go.uuid"
)

func resourceProjectQuota() *schema.Resource {
return &schema.Resource{
Create: resourceProjectQuotaCreate,
Read: resourceProjectQuotaRead,
Update: resourceProjectQuotaUpdate,
Delete: resourceProjectQuotaDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Read: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

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

func resourceProjectQuotaCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(uuid.NewV4().String())
return resourceProjectQuotaUpdate(d, meta)
}

func resourceProjectQuotaRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
projectClient := config.SandwichClient.Project()

quota, err := projectClient.GetQuota()
if err != nil {
if apiError, ok := err.(api.APIErrorInterface); ok {
if apiError.IsNotFound() {
d.SetId("")
return nil
}
}
}
d.Set("vcpu", quota.VCPU)
d.Set("ram", quota.Ram)
d.Set("disk", quota.Disk)
return nil
}

func resourceProjectQuotaUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
projectClient := config.SandwichClient.Project()
vcpu := d.Get("vcpu").(int)
ram := d.Get("ram").(int)
disk := d.Get("disk").(int)

err := projectClient.SetQuota(vcpu, ram, disk)
if err != nil {
return err
}
return resourceProjectQuotaRead(d, meta)
}

func resourceProjectQuotaDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

0 comments on commit 2ebdb9a

Please sign in to comment.