Skip to content

Commit

Permalink
add datasource harbor_groups (#352) (#387)
Browse files Browse the repository at this point in the history
fixes #352

Signed-off-by: flbla <[email protected]>
  • Loading branch information
flbla authored Oct 20, 2023
1 parent a5e9a01 commit 6aecdd7
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/data-sources/groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Data Source: harbor_groups

## Example Usage
```hcl
data "harbor_groups" "example" {
group_name = "example-group"
}
output "group_ids" {
value = [data.harbor_groups.example.*.id]
}
```

## Argument Reference
The following arguments are supported:

* `group_name` - (Optional) The name of the group to filter by.
* `ldap_group_dn` - (Optional) The LDAP group DN to filter by.

## Attributes Reference
In addition to all arguments, the following attributes are exported:

* `groups` - (Computed) A list of groups matching the previous arguments. Each `group` object provides the attributes documented below.

---

**group** object exports the following:

* `id` - The ID of the group.
* `group_name` - The name of the group.
* `group_type` - The type of the group.
* `ldap_group_dn` - The LDAP group DN of the group.

This data source retrieves a list of Harbor groups and filters them based on the `group_name` and `ldap_group_dn` arguments. It returns a list of `group` objects, each containing the `id`, `group_name`, `group_type`, and `ldap_group_dn` attributes of a group that matches the filter criteria.
101 changes: 101 additions & 0 deletions provider/data_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package provider

import (
"encoding/json"
"fmt"
"strconv"

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

func dataGroups() *schema.Resource {
return &schema.Resource{
Read: dataGroupsRead,
Schema: map[string]*schema.Schema{
"group_name": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"ldap_group_dn": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_name": {
Type: schema.TypeString,
Computed: true,
},
"group_type": {
Type: schema.TypeInt,
Computed: true,
},
"id": {
Type: schema.TypeInt,
Computed: true,
},
"ldap_group_dn": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

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

groupName := d.Get("group_name").(string)
ldapGroupDN := d.Get("ldap_group_dn").(string)

page := 1
userGroupsData := make([]map[string]interface{}, 0)
for {
resp, _, _, err := apiClient.SendRequest("GET", models.PathGroups+"?page="+strconv.Itoa(page), nil, 200)
if err != nil {
return err
}

var jsonData []models.GroupBody
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return fmt.Errorf("unable to retrieve Harbor user groups data: %s", err)
}

// If there is no data on the current page, we have reached the last page
if len(jsonData) == 0 {
break
}

for _, v := range jsonData {
if (groupName == "" || v.Groupname == groupName) &&
(ldapGroupDN == "" || v.LdapGroupDn == ldapGroupDN) {

userGroupData := map[string]interface{}{
"group_name": v.Groupname,
"group_type": v.GroupType,
"id": v.ID,
"ldap_group_dn": v.LdapGroupDn,
}

userGroupsData = append(userGroupsData, userGroupData)
}
}

page++
}
d.SetId("harbor-user-groups")
d.Set("groups", userGroupsData)

return nil
}
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func Provider() *schema.Provider {
"harbor_project": dataProject(),
"harbor_projects": dataProjects(),
"harbor_registry": dataRegistry(),
"harbor_groups": dataGroups(),
},

ConfigureFunc: providerConfigure,
Expand Down

0 comments on commit 6aecdd7

Please sign in to comment.