Skip to content

Commit

Permalink
initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
SivaanandM committed Nov 7, 2024
1 parent 8ef7f1e commit 16f0bda
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions spectrocloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func New(_ string) func() *schema.Provider {
"spectrocloud_workspace": resourceWorkspace(),
"spectrocloud_alert": resourceAlert(),
"spectrocloud_ssh_key": resourceSSHKey(),
"spectrocloud_user": resourceUser(),
},
DataSourcesMap: map[string]*schema.Resource{
"spectrocloud_team": dataSourceTeam(),
Expand Down
148 changes: 148 additions & 0 deletions spectrocloud/resource_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package spectrocloud

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"regexp"
"time"

"github.com/spectrocloud/palette-sdk-go/api/models"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceUser() *schema.Resource {
return &schema.Resource{
CreateContext: resourceUserCreate,
ReadContext: resourceUserRead,
UpdateContext: resourceUserUpdate,
DeleteContext: resourceUserDelete,
Description: "Create and manage projects in Palette.",

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

SchemaVersion: 2,
Schema: map[string]*schema.Schema{
"first_name": {
Type: schema.TypeString,
Required: true,
Description: "The first name of the user.",
},
"last_name": {
Type: schema.TypeString,
Required: true,
Description: "The last name of the user.",
},
"email": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`),
"must be a valid email address",
),
Description: "The email of the user.",
},
"role_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The roles id's assigned to the user.",
},
"team_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The roles id's assigned to the user.",
},
},
}
}

func resourceUserCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

c := getV1ClientWithResourceContext(m, "")
var diags diag.Diagnostics

uid, err := c.CreateUser(toUser(d))
if err != nil {
return diag.FromErr(err)
}
d.SetId(uid)

return diags
}

func resourceUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

//c := getV1ClientWithResourceContext(m, "")
var diags diag.Diagnostics

//project, err := c.GetProject(d.Id())
//if err != nil {
// return diag.FromErr(err)
//} else if project == nil {
// // Deleted - Terraform will recreate it
// d.SetId("")
// return diags
//}
//
//if err := d.Set("name", project.Metadata.Name); err != nil {
// return diag.FromErr(err)
//}
//
//if v, found := project.Metadata.Annotations["description"]; found {
// if err := d.Set("description", v); err != nil {
// return diag.FromErr(err)
// }
//}
//
//if err := d.Set("tags", flattenTags(project.Metadata.Labels)); err != nil {
// return diag.FromErr(err)
//}

return diags
}

func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

//c := getV1ClientWithResourceContext(m, "")
var diags diag.Diagnostics

//err := c.UpdateProject(d.Id(), toProject(d))
//if err != nil {
// return diag.FromErr(err)
//}
return diags
}

func resourceUserDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

//c := getV1ClientWithResourceContext(m, "")
var diags diag.Diagnostics

//err := c.DeleteProject(d.Id())
//if err != nil {
// return diag.FromErr(err)
//}

return diags
}

func toUser(d *schema.ResourceData) *models.V1UserEntity {
return &models.V1UserEntity{
Metadata: &models.V1ObjectMeta{},
Spec: &models.V1UserSpecEntity{
EmailID: d.Get("email").(string),
FirstName: d.Get("first_name").(string),
LastName: d.Get("last_name").(string),
Roles: nil,
Teams: nil,
},
}
}

0 comments on commit 16f0bda

Please sign in to comment.