From 8be0dba17910a1556d1bc54b86a351c4b77218b1 Mon Sep 17 00:00:00 2001 From: Hai Wu Date: Tue, 17 May 2022 00:57:27 -0400 Subject: [PATCH] add support for OIDC groups --- client/group.go | 14 +++++++ docs/resources/group.md | 24 +++++++++++ models/group.go | 10 +++++ provider/provider.go | 1 + provider/resource_group.go | 84 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 client/group.go create mode 100644 docs/resources/group.md create mode 100644 models/group.go create mode 100644 provider/resource_group.go diff --git a/client/group.go b/client/group.go new file mode 100644 index 0000000..58db13e --- /dev/null +++ b/client/group.go @@ -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), + } +} diff --git a/docs/resources/group.md b/docs/resources/group.md new file mode 100644 index 0000000..97cb648 --- /dev/null +++ b/docs/resources/group.md @@ -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 +` diff --git a/models/group.go b/models/group.go new file mode 100644 index 0000000..c0849f6 --- /dev/null +++ b/models/group.go @@ -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"` +} diff --git a/provider/provider.go b/provider/provider.go index 5ef3bc2..62a220e 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -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(), diff --git a/provider/resource_group.go b/provider/resource_group.go new file mode 100644 index 0000000..b49af52 --- /dev/null +++ b/provider/resource_group.go @@ -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 +}