From c4df0fe534c0affe134312b291fc5f4b8026f6f8 Mon Sep 17 00:00:00 2001 From: Ivan Savciuc Date: Thu, 28 Dec 2023 16:51:26 +0200 Subject: [PATCH] feat: add project organization handler --- client.go | 2 + go.sum | 3 ++ project.go | 1 + project_organization_groups.go | 74 ++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 project_organization_groups.go diff --git a/client.go b/client.go index 442ca34..95dc754 100644 --- a/client.go +++ b/client.go @@ -96,6 +96,7 @@ type Client struct { OrganizationUserGroupMembers *OrganizationUserGroupMembersHandler OpenSearchSecurityPluginHandler *OpenSearchSecurityPluginHandler OpenSearchACLs *OpenSearchACLsHandler + ProjectOrganization *ProjectOrgHandler } // GetUserAgentOrDefault configures a default userAgent value, if one has not been provided. @@ -272,6 +273,7 @@ func (c *Client) Init() { c.OrganizationUserGroupMembers = &OrganizationUserGroupMembersHandler{c} c.OpenSearchSecurityPluginHandler = &OpenSearchSecurityPluginHandler{c} c.OpenSearchACLs = &OpenSearchACLsHandler{c} + c.ProjectOrganization = &ProjectOrgHandler{c} } func (c *Client) doGetRequest(ctx context.Context, endpoint string, req interface{}) ([]byte, error) { diff --git a/go.sum b/go.sum index 74c6328..eaadf09 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,7 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -31,6 +32,7 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108 github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/ginkgo/v2 v2.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc= +github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= @@ -85,6 +87,7 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= diff --git a/project.go b/project.go index 0da60c8..cc65f68 100644 --- a/project.go +++ b/project.go @@ -27,6 +27,7 @@ type ( BillingGroupId string `json:"billing_group_id"` BillingGroupName string `json:"billing_group_name"` Tags map[string]string `json:"tags"` + OrganizationId string `json:"organization_id"` } // ProjectsHandler is the client which interacts with the Projects endpoints diff --git a/project_organization_groups.go b/project_organization_groups.go new file mode 100644 index 0000000..cf08fa8 --- /dev/null +++ b/project_organization_groups.go @@ -0,0 +1,74 @@ +package aiven + +import "context" + +// ProjectOrgHandler is the client that interacts with +// projects and organizations relations on Aiven. +type ProjectOrgHandler struct { + client *Client +} + +// ProjectUserGroupRequest is the request from Aiven for the project user group endpoints. +type ProjectUserGroupRequest struct { + Role string `json:"role"` +} + +// ProjectListResponse is the response from Aiven for the project endpoints. +type ProjectUserGroupListResponse struct { + APIResponse + AccessList []*ProjectUserGroup `json:"resource_access_list"` +} + +// ProjectUserGroup is the response from Aiven for the project user group endpoints. +type ProjectUserGroup struct { + OrganizationGroupID string `json:"user_group_id"` + Role string `json:"role"` + CreateTime string `json:"create_time"` + UpdateTime string `json:"update_time"` +} + +// Add or update direct access to a project for a group with a given role. +// API endpoint: PUT /project//access/groups/ +func (h *ProjectOrgHandler) Add(ctx context.Context, project, userGroupID, role string) error { + path := buildPath("project", project, "access", "groups", userGroupID) + _, err := h.client.doPutRequest(ctx, path, ProjectUserGroupRequest{Role: role}) + return err +} + +// Delete removes direct access to a project for a group. +// API endpoint: DELETE /project//access/groups/ +func (h *ProjectOrgHandler) Delete(ctx context.Context, project, userGroupID string) error { + path := buildPath("project", project, "access", "groups", userGroupID) + _, err := h.client.doDeleteRequest(ctx, path, nil) + return err +} + +// List all project that have a direct access for an organization. +// API endpoint: GET /organization/{organization_id}/projects +func (h *ProjectOrgHandler) OrganizationProjects(ctx context.Context, organizationID string) ([]*Project, error) { + path := buildPath("organization", organizationID, "projects") + bts, err := h.client.doGetRequest(ctx, path, nil) + if err != nil { + return nil, err + } + + var r ProjectListResponse + errR := checkAPIResponse(bts, &r) + + return r.Projects, errR +} + +// Retrieve the list of resource access entries for the given project. +// API endpoint: GET /project//access +func (h *ProjectOrgHandler) List(ctx context.Context, project string) ([]*ProjectUserGroup, error) { + path := buildPath("project", project, "access") + bts, err := h.client.doGetRequest(ctx, path, nil) + if err != nil { + return nil, err + } + + var r ProjectUserGroupListResponse + errR := checkAPIResponse(bts, &r) + + return r.AccessList, errR +}