-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from haiwu/issue_216
add support for OIDC groups
- Loading branch information
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |