Skip to content

Commit

Permalink
Merge pull request #217 from haiwu/issue_216
Browse files Browse the repository at this point in the history
add support for OIDC groups
  • Loading branch information
wrighbr authored May 17, 2022
2 parents baa0015 + 8be0dba commit f743a7a
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
14 changes: 14 additions & 0 deletions client/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package client

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

// GroupBody return a json body
func GroupBody(d *schema.ResourceData) models.GroupBody {
return models.GroupBody{
Groupname: d.Get("group_name").(string),
GroupType: d.Get("group_type").(int),
}
}
24 changes: 24 additions & 0 deletions docs/resources/group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Resource: harbor_group

## Example Usage
```hcl
resource "harbor_group" "storage-group" {
group_name = "storage-group"
group_type = 3
}
```

## Argument Reference
The following arguments are supported:

* **group_name** - (Required) The name of the group.

* **group_type** - (Required) 3. Note: group type 3 is OIDC group.

## Import
An OIDC group can be imported using the `group id` eg,

`
terraform import harbor_group.storage-group /usergroups/19
`
10 changes: 10 additions & 0 deletions models/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

var PathGroups = "/usergroups"

//
type GroupBody struct {
Groupname string `json:"group_name,omitempty"`
GroupType int `json:"group_type,omitempty"`
ID int `json:"id,omitempty"`
}
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Provider() *schema.Provider {
"harbor_interrogation_services": resourceVuln(),
"harbor_robot_account": resourceRobotAccount(),
"harbor_user": resourceUser(),
"harbor_group": resourceGroup(),
"harbor_registry": resourceRegistry(),
"harbor_replication": resourceReplication(),
"harbor_retention_policy": resourceRetention(),
Expand Down
84 changes: 84 additions & 0 deletions provider/resource_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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/v2/helper/schema"
)

func resourceGroup() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"group_type": {
Type: schema.TypeInt,
Required: true,
},
},
Create: resourceGroupCreate,
Read: resourceGroupRead,
Update: resourceGroupUpdate,
Delete: resourceGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

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

body := client.GroupBody(d)

_, header, err := apiClient.SendRequest("POST", models.PathGroups, &body, 201)
if err != nil {
return err
}

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

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

func resourceGroupRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)
resp, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
return err
}
var jsonData models.GroupBody
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return fmt.Errorf("Resource not found %s", d.Id())
}

d.Set("group_name", jsonData.Groupname)
d.Set("group_type", jsonData.GroupType)

return nil
}

func resourceGroupUpdate(d *schema.ResourceData, m interface{}) error {
return resourceGroupRead(d, m)
}

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

_, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200)
if err != nil {
return err
}
return nil
}

0 comments on commit f743a7a

Please sign in to comment.