Skip to content

Commit

Permalink
Finish Identity View (#30)
Browse files Browse the repository at this point in the history
This has identified a number of UX problems, in that we can list
identities for an organization, but then we need to get regions per
identity (project) to correctly scope regions to translate from ID to
name.  I suspect that perhaps regions should be defined at the
organization level - not the project, identities should have a regionID
property much like clusters, then we can handle those in a geenric
fashion and promote region scoped metadata to a top level core thing.
  • Loading branch information
spjmurray authored Jul 10, 2024
1 parent 8135ebe commit e228e66
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 53 deletions.
55 changes: 45 additions & 10 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"slices"
"time"

coreconstants "github.com/unikorn-cloud/core/pkg/constants"
coreapi "github.com/unikorn-cloud/core/pkg/openapi"
"github.com/unikorn-cloud/core/pkg/server/conversion"
"github.com/unikorn-cloud/core/pkg/server/errors"
Expand All @@ -41,6 +42,7 @@ import (
"github.com/unikorn-cloud/region/pkg/server/util"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -277,23 +279,56 @@ func convertIdentity(identity *unikornv1.Identity, in *providers.CloudConfig) *o
out.Spec.Tags = &tags
}

switch in.Type {
case providers.ProviderTypeOpenStack:
out.Spec = openapi.IdentitySpec{
Type: openapi.Openstack,
Openstack: &openapi.IdentitySpecOpenStack{
Cloud: in.OpenStack.Credentials.Cloud,
CloudConfig: base64.URLEncoding.EncodeToString(in.OpenStack.Credentials.CloudConfig),
UserId: in.OpenStack.State.UserID,
ProjectId: in.OpenStack.State.ProjectID,
},
switch identity.Spec.Provider {
case unikornv1.ProviderOpenstack:
out.Spec.Type = openapi.Openstack

out.Spec.Openstack = &openapi.IdentitySpecOpenStack{
UserId: identity.Spec.OpenStack.UserID,
ProjectId: identity.Spec.OpenStack.ProjectID,
}

if in != nil {
cloudConfig := base64.URLEncoding.EncodeToString(in.OpenStack.Credentials.CloudConfig)

out.Spec.Openstack.Cloud = &in.OpenStack.Credentials.Cloud
out.Spec.Openstack.CloudConfig = &cloudConfig
}
}

return out
}

func convertIdentityList(in unikornv1.IdentityList) openapi.IdentitiesRead {
out := make(openapi.IdentitiesRead, len(in.Items))

for i := range in.Items {
out[i] = *convertIdentity(&in.Items[i], nil)
}

return out
}

func (h *Handler) GetApiV1OrganizationsOrganizationIDIdentities(w http.ResponseWriter, r *http.Request, organizationID openapi.OrganizationIDParameter) {
if err := rbac.AllowOrganizationScope(r.Context(), "infrastructure", identityapi.Read, organizationID); err != nil {
errors.HandleError(w, r, err)
return
}

var resources unikornv1.IdentityList

options := &client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{
coreconstants.OrganizationLabel: organizationID,
}),
}

if err := h.client.List(r.Context(), &resources, options); err != nil {
errors.HandleError(w, r, errors.OAuth2ServerError("unable to list identities").WithError(err))
return
}

util.WriteJSONResponse(w, r, http.StatusOK, convertIdentityList(resources))
}

func (h *Handler) PostApiV1OrganizationsOrganizationIDProjectsProjectIDRegionsRegionIDIdentities(w http.ResponseWriter, r *http.Request, organizationID openapi.OrganizationIDParameter, projectID openapi.ProjectIDParameter, regionID openapi.RegionIDParameter) {
Expand Down
78 changes: 39 additions & 39 deletions pkg/openapi/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pkg/openapi/server.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,6 @@ components:
description: Everything an OpenStack client needs to function.
type: object
required:
- cloud
- cloudConfig
- userId
- projectId
properties:
Expand Down
4 changes: 2 additions & 2 deletions pkg/openapi/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e228e66

Please sign in to comment.