-
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.
Browse files
Browse the repository at this point in the history
fixes #352 Signed-off-by: flbla <[email protected]>
- Loading branch information
Showing
3 changed files
with
136 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,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. |
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,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 | ||
} |
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