Skip to content

Commit

Permalink
Merge pull request #13 from BESTSELLER/ES-984-Refactoring
Browse files Browse the repository at this point in the history
Es 984 refactoring
  • Loading branch information
Brett Wright authored Sep 4, 2020
2 parents 9383cbf + 8d51e0e commit ed8dab8
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 11 deletions.
13 changes: 11 additions & 2 deletions client/project_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func ProjectMembersBody(d *schema.ResourceData) models.ProjectMembersBody {
func ProjectMembersGroupBody(d *schema.ResourceData) models.ProjectMembersBody {
return models.ProjectMembersBody{
RoleID: RoleType(d.Get("role").(string)),
GroupMember: models.ProjectMembersBodyGroup{
GroupType: GroupType(d.Get("type").(string)),
GroupName: d.Get("name").(string),
GroupName: d.Get("group_name").(string),
},
}
}

func ProjectMembersUserBody(d *schema.ResourceData) models.ProjectMembersBody {
return models.ProjectMembersBody{
RoleID: RoleType(d.Get("role").(string)),
UserMembers: models.ProjectMemberUsersGroup{
UserName: d.Get("user_name").(string),
},
}
}
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/project_member.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Resource: harbor_project_member

### WARNING - Resource project_member has been renamed to project_member_group. This resource is deprecated and will be removed in the next major version

## Example Usage
```
resource "haror_project" "main" {
Expand Down
29 changes: 29 additions & 0 deletions docs/resources/project_member_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Resource: harbor_project_member_group

## Example Usage
```
resource "haror_project" "main" {
name = "main"
}
resource "harbor_project_member_group" "main" {
project_id = harbor_project.main.id
name = "testing1"
role = "master"
type = "oidc"
}
```

## Argument Reference
The following arguments are supported:

* **name** - (Required) The name of the member entity

* **project_id** - (Required) The project id of the project that the entity will have access to.

* **role** - (Required) The premissions that the entity will be granted.

* **type** - (Requried) The group type. Can be set to **"ldap"**, **"internal"** or **"oidc"**

`NOTE: odic group type can only be used with harbor version v1.10.1 and above`
24 changes: 24 additions & 0 deletions docs/resources/project_member_user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Resource: harbor_project_member_user

## Example Usage
```
resource "haror_project" "main" {
name = "main"
}
resource "harbor_project_member_user" "main" {
project_id = harbor_project.main.id
user_name = "testing1"
role = "master"
}
```

## Argument Reference
The following arguments are supported:

* **user_name** - (Required) The name of the member entity

* **project_id** - (Required) The project id of the project that the entity will have access to.

* **role** - (Required) The premissions that the entity will be granted.
5 changes: 5 additions & 0 deletions models/project_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ type ProjectMembersBody 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 ProjectMembersBodyGroup struct {
GroupType int `json:"group_type,omitempty"`
GroupName string `json:"group_name,omitempty"`
}

type ProjectMemberUsersGroup struct {
UserName string `json:"username,omitempty"`
}
2 changes: 2 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func Provider() terraform.ResourceProvider {
"harbor_config_system": resourceConfigSystem(),
"harbor_project": resourceProject(),
"harbor_project_member": resourceMembers(),
"harbor_project_member_group": resourceMembersGroup(),
"harbor_project_member_user": resourceMembersUser(),
"harbor_tasks": resourceTasks(),
"harbor_interrogation_services": resourceVuln(),
"harbor_robot_account": resourceRobotAccount(),
Expand Down
14 changes: 8 additions & 6 deletions provider/resource_project_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func resourceMembers() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{

"project_id": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -49,10 +50,11 @@ func resourceMembers() *schema.Resource {
},
},
},
Create: resourceMembersCreate,
Read: resourceMembersRead,
Update: resourceMembersUpdate,
Delete: resourceMembersDelete,
Create: resourceMembersCreate,
Read: resourceMembersRead,
Update: resourceMembersUpdate,
Delete: resourceMembersDelete,
DeprecationMessage: "The resource project_member has been renamed to project_member_group. This resource is deprecated and will be removed in the next major version",
}
}

Expand All @@ -61,7 +63,7 @@ func resourceMembersCreate(d *schema.ResourceData, m interface{}) error {
projectid := checkProjectid(d.Get("project_id").(string))
path := projectid + "/members"

body := client.ProjectBody(d)
body := client.ProjectMembersGroupBody(d)

_, headers, err := apiClient.SendRequest("POST", path, body, 201)
if err != nil {
Expand Down Expand Up @@ -98,7 +100,7 @@ func resourceMembersRead(d *schema.ResourceData, m interface{}) error {
func resourceMembersUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

body := client.ProjectMembersBody(d)
body := client.ProjectMembersGroupBody(d)
_, _, err := apiClient.SendRequest("GET", d.Id(), body, 200)
if err != nil {
fmt.Println(err)
Expand Down
118 changes: 118 additions & 0 deletions provider/resource_project_member_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package provider

import (
"encoding/json"
"fmt"

"github.com/BESTSELLER/terraform-provider-harbor/client"
"github.com/BESTSELLER/terraform-provider-harbor/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceMembersGroup() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"member_id": {
Type: schema.TypeInt,
Computed: true,
},
"role": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if v != "projectadmin" && v != "developer" && v != "guest" && v != "master" {
errs = append(errs, fmt.Errorf("%q must be either projectadmin, developer, guest or master, got: %s", key, v))
}
return
},
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if v != "ldap" && v != "internal" && v != "oidc" {
errs = append(errs, fmt.Errorf("%q must be either ldap, internal or oidc, got: %s", key, v))
}
return
},
},
},
Create: resourceMembersGroupCreate,
Read: resourceMembersGroupRead,
Update: resourceMembersGroupUpdate,
Delete: resourceMembersGroupDelete,
}
}

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

body := client.ProjectBody(d)

_, headers, err := apiClient.SendRequest("POST", path, body, 201)
if err != nil {
return err
}

id, err := client.GetID(headers)
if err != nil {
return err
}

d.SetId(id)
return resourceMembersGroupRead(d, m)
}

func resourceMembersGroupRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
fmt.Println(err)
}

var jsonData models.ProjectMembersBody
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return err
}

d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
return nil
}

func resourceMembersGroupUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

body := client.ProjectMembersUserBody(d)
_, _, err := apiClient.SendRequest("GET", d.Id(), body, 200)
if err != nil {
fmt.Println(err)
}

return resourceMembersGroupRead(d, m)
}

func resourceMembersGroupDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

_, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200)
if err != nil {
fmt.Println(err)
}
return nil
}
106 changes: 106 additions & 0 deletions provider/resource_project_member_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package provider

import (
"encoding/json"
"fmt"

"github.com/BESTSELLER/terraform-provider-harbor/client"
"github.com/BESTSELLER/terraform-provider-harbor/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceMembersUser() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"user_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"member_id": {
Type: schema.TypeInt,
Computed: true,
},
"role": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if v != "projectadmin" && v != "developer" && v != "guest" && v != "master" {
errs = append(errs, fmt.Errorf("%q must be either projectadmin, developer, guest or master, got: %s", key, v))
}
return
},
},
},
Create: resourceMembersUserCreate,
Read: resourceMembersUserRead,
Update: resourceMembersUserUpdate,
Delete: resourceMembersUserDelete,
}
}

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

body := client.ProjectMembersUserBody(d)

_, headers, err := apiClient.SendRequest("POST", path, body, 201)
if err != nil {
return err
}

id, err := client.GetID(headers)
if err != nil {
return err
}

d.SetId(id)
return resourceMembersUserRead(d, m)
}

func resourceMembersUserRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
fmt.Println(err)
}

var jsonData models.ProjectMembersBody
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return err
}

d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
return nil
}

func resourceMembersUserUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

body := client.ProjectMembersUserBody(d)
_, _, err := apiClient.SendRequest("PUT", d.Id(), body, 200)
if err != nil {
fmt.Println(err)
}

return resourceMembersUserRead(d, m)
}

func resourceMembersUserDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

_, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200)
if err != nil {
fmt.Println(err)
}
return nil
}
8 changes: 5 additions & 3 deletions provider/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ func resourceUserUpdate(d *schema.ResourceData, m interface{}) error {
return err
}

_, _, err = apiClient.SendRequest("PUT", d.Id()+"/password", body, 200)
if err != nil {
return err
if d.HasChange("password") == true {
_, _, err = apiClient.SendRequest("PUT", d.Id()+"/password", body, 200)
if err != nil {
return err
}
}

return resourceUserRead(d, m)
Expand Down

0 comments on commit ed8dab8

Please sign in to comment.