-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
219 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |