Skip to content

Commit

Permalink
Merge pull request #172 from sshota0809/issue/168
Browse files Browse the repository at this point in the history
Modify api path to add member to project
  • Loading branch information
wrighbr authored Dec 14, 2021
2 parents 8c03b0e + 0d128aa commit c26a662
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 14 deletions.
8 changes: 4 additions & 4 deletions client/project_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ProjectMembersGroupBody(d *schema.ResourceData) models.ProjectMembersBody {
body := models.ProjectMembersBody{
func ProjectMembersGroupBody(d *schema.ResourceData) models.ProjectMembersBodyPost {
body := models.ProjectMembersBodyPost{
RoleID: RoleType(d.Get("role").(string)),
GroupMember: models.ProjectMembersBodyGroup{
GroupType: GroupType(d.Get("type").(string)),
Expand All @@ -21,8 +21,8 @@ func ProjectMembersGroupBody(d *schema.ResourceData) models.ProjectMembersBody {
return body
}

func ProjectMembersUserBody(d *schema.ResourceData) models.ProjectMembersBody {
return models.ProjectMembersBody{
func ProjectMembersUserBody(d *schema.ResourceData) models.ProjectMembersBodyPost {
return models.ProjectMembersBodyPost{
RoleID: RoleType(d.Get("role").(string)),
UserMembers: models.ProjectMemberUsersGroup{
UserName: d.Get("user_name").(string),
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/project_member_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ resource "harbor_project" "main" {
}
resource "harbor_project_member_group" "main" {
project_id = harbor_project.main.id
project_id = harbor_project.main.project_id
group_name = "testing1"
role = "master"
type = "oidc"
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/project_member_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ resource "harbor_project" "main" {
}
resource "harbor_project_member_user" "main" {
project_id = harbor_project.main.id
project_id = harbor_project.main.project_id
user_name = "testing1"
role = "master"
}
Expand Down
12 changes: 11 additions & 1 deletion models/project_members.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package models

type ProjectMembersBody struct {
type ProjectMembersBodyPost struct {
ID int `json:"id,omitempty"`
RoleID int `json:"role_id,omitempty"`
GroupMember ProjectMembersBodyGroup `json:"member_group,omitempty"`
UserMembers ProjectMemberUsersGroup `json:"member_user,omitempty"`
}

type ProjectMembersBodyResponses struct {
ID int `json:"id,omitempty"`
RoleID int `json:"role_id,omitempty"`
ProjectID int `json:"project_id,omitempty"`
EntityType string `json:"entity_type,omitempty"`
EntityName string `json:"entity_name,omitempty"`
}

type ProjectMembersBodyGroup struct {
GroupType int `json:"group_type,omitempty"`
GroupName string `json:"group_name,omitempty"`
Expand All @@ -17,3 +25,5 @@ type ProjectMembersBodyGroup struct {
type ProjectMemberUsersGroup struct {
UserName string `json:"username,omitempty"`
}


5 changes: 4 additions & 1 deletion provider/resource_project_member_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"encoding/json"
"fmt"
"strconv"

"github.com/BESTSELLER/terraform-provider-harbor/client"
"github.com/BESTSELLER/terraform-provider-harbor/models"
Expand Down Expand Up @@ -96,13 +97,15 @@ func resourceMembersGroupRead(d *schema.ResourceData, m interface{}) error {
return nil
}

var jsonData models.ProjectMembersBody
var jsonData models.ProjectMembersBodyResponses
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return fmt.Errorf("Resource not found %s", d.Id())
}

d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
d.Set("project_id", strconv.Itoa(jsonData.ProjectID))
d.Set("group_name", jsonData.EntityName)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions provider/resource_project_member_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func testAccCheckMemberGroupBasic(projectName string, groupName string) string {
}
resource "harbor_project_member_group" "main" {
project_id = harbor_project.main.id
project_id = harbor_project.main.project_id
group_name = "%s"
role = "developer"
type = "oidc"
Expand All @@ -104,7 +104,7 @@ func testAccCheckMemberGroupUpdate(projectName string, groupName string) string
}
resource "harbor_project_member_group" "main" {
project_id = harbor_project.main.id
project_id = harbor_project.main.project_id
group_name = "%s"
role = "guest"
type = "oidc"
Expand Down
8 changes: 6 additions & 2 deletions provider/resource_project_member_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"encoding/json"
"fmt"
"strconv"

"github.com/BESTSELLER/terraform-provider-harbor/client"
"github.com/BESTSELLER/terraform-provider-harbor/models"
Expand Down Expand Up @@ -47,7 +48,8 @@ func resourceMembersUser() *schema.Resource {

func resourceMembersUserCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)
path := d.Get("project_id").(string) + "/members"
projectid := checkProjectid(d.Get("project_id").(string))
path := projectid + "/members"

body := client.ProjectMembersUserBody(d)

Expand All @@ -74,13 +76,15 @@ func resourceMembersUserRead(d *schema.ResourceData, m interface{}) error {
return nil
}

var jsonData models.ProjectMembersBody
var jsonData models.ProjectMembersBodyResponses
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return fmt.Errorf("Resource not found %s", d.Id())
}

d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
d.Set("project_id", strconv.Itoa(jsonData.ProjectID))
d.Set("user_name", jsonData.EntityName)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions provider/resource_project_member_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func testAccCheckMemberUserBasic() string {
}
resource "harbor_project_member_user" "main" {
project_id = harbor_project.main.id
project_id = harbor_project.main.project_id
role = "developer"
user_name = harbor_user.main.username
}
Expand All @@ -111,7 +111,7 @@ func testAccCheckMemberUserUpdate() string {
}
resource "harbor_project_member_user" "main" {
project_id = harbor_project.main.id
project_id = harbor_project.main.project_id
role = "guest"
user_name = harbor_user.main.username
}
Expand Down

0 comments on commit c26a662

Please sign in to comment.