Skip to content

Commit

Permalink
schema design completed
Browse files Browse the repository at this point in the history
  • Loading branch information
SivaanandM committed Nov 7, 2024
1 parent 16f0bda commit 508526e
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 45 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)

//replace github.com/spectrocloud/palette-sdk-go => ../palette-sdk-go
replace github.com/spectrocloud/palette-sdk-go => ../palette-sdk-go
247 changes: 203 additions & 44 deletions spectrocloud/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,128 @@ func resourceUser() *schema.Resource {
),
Description: "The email of the user.",
},
"role_ids": {
"teams": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The roles id's assigned to the user.",
Description: "The team 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.",
"project_role_mapping": {
Type: schema.TypeSet,
Set: resourceProjectRoleMappingHash,
Optional: true,
ConflictsWith: []string{"teams"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "Project id to be associated with the user.",
},
"roles": {
Type: schema.TypeSet,
Required: true,
Set: schema.HashString,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of project role ids to be associated with the user. ",
},
},
},
Description: "List of project roles to be associated with the user. ",
},
"tenant_role_mapping": {
Type: schema.TypeSet,
Optional: true,
ConflictsWith: []string{"teams"},
Set: schema.HashString,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of tenant role ids to be associated with the user. ",
},
"workspace_role_mapping": {
Type: schema.TypeSet,
ConflictsWith: []string{"teams"},
Set: resourceWorkspaceRoleMappingHash,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "Project id to be associated with the user.",
},
"workspace": {
Type: schema.TypeSet,
Set: resourceProjectRoleMappingHash,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "Workspace id to be associated with the user.",
},
"roles": {
Type: schema.TypeSet,
Set: schema.HashString,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of workspace role ids to be associated with the user.",
},
},
},
Description: "List of workspace roles to be associated with the user. ",
},
},
},
Description: "List of workspace roles to be associated with the user. ",
},
"resource_role_mapping": {
Type: schema.TypeSet,
ConflictsWith: []string{"teams"},
Set: resourceWorkspaceRoleMappingHash,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "Project id to be associated with the user.",
},
"filters": {
Type: schema.TypeSet,
Set: schema.HashString,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of filter ids.",
},
"roles": {
Type: schema.TypeSet,
Set: schema.HashString,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of resource role ids to be associated with the user.",
},
},
},
},
},
}
}

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

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

uid, err := c.CreateUser(toUser(d))
Expand All @@ -78,71 +181,127 @@ func resourceUserCreate(ctx context.Context, d *schema.ResourceData, m interface
return diags
}

func toProjectRoleMapping(d *schema.ResourceData) *models.V1ProjectRolesEntity {
if projectRoles, ok := d.GetOk("project_role_mapping"); ok && projectRoles != nil {
var role *models.V1ProjectRolesEntity
projects := make([]*models.V1UIDRoleSummary, 0)
for _, r := range projectRoles.([]interface{}) {
rids := make([]*models.V1UIDSummary, 0)
for _, id := range r.(map[string]interface{})["roles"].(*schema.Set).List() {
rids = append(rids, &models.V1UIDSummary{
UID: id.(string),
})
}

projects = append(projects, &models.V1UIDRoleSummary{
Roles: rids,
UID: r.(map[string]interface{})["id"].(string),
})
}
role.Projects = projects
return role
}

return nil
}

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

//c := getV1ClientWithResourceContext(m, "")
c := getV1ClientWithResourceContext(m, "tenant")
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)
//}

email := d.Get("email").(string)
user, err := c.GetUserSummaryByEmail(email)
if err != nil {
return diag.FromErr(err)
} else if user == nil {
// Deleted - Terraform will recreate it
d.SetId("")
return diags
}
err = flattenUser(user, d)
if err != nil {
return diag.FromErr(err)
}
return diags
}

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

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

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

}
if d.HasChanges("tenant_role_mapping") {

}
if d.HasChanges("workspace_role_mapping") {

}
if d.HasChanges("resource_role_mapping") {

}

return diags
}

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

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

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

return diags
}

func flattenUser(user *models.V1UserSummary, d *schema.ResourceData) error {
if user != nil {
if err := d.Set("first_name", user.Spec.FirstName); err != nil {
return err
}
if err := d.Set("last_name", user.Spec.LastName); err != nil {
return err
}
if err := d.Set("email", user.Spec.EmailID); err != nil {
return err
}
if user.Spec.Roles != nil {
var roleIds []string
for _, role := range user.Spec.Roles {
roleIds = append(roleIds, role.UID)
}
if err := d.Set("role_ids", roleIds); err != nil {
return err
}
}
if user.Spec.Teams != nil {
var teamIds []string
for _, team := range user.Spec.Teams {
teamIds = append(teamIds, team.UID)
}
if err := d.Set("teams", teamIds); err != nil {
return err
}
}
}

return nil
}

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,
Teams: d.Get("teams").([]string),
},
}
}

0 comments on commit 508526e

Please sign in to comment.