Skip to content

Commit

Permalink
Remove required attribute from tenant.Id and cluster.Id
Browse files Browse the repository at this point in the history
The codegen does now correctly detect required attributes and fails if no id is present.
Ids are not required for `create` and set with the URL parameters for all other requsts.
  • Loading branch information
bastjan committed May 27, 2024
1 parent 6804d53 commit bb052e4
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 104 deletions.
4 changes: 0 additions & 4 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ components:
example: v1.2.3
TenantId:
type: object
required:
- id
properties:
id:
$ref: '#/components/schemas/Id'
Expand Down Expand Up @@ -138,8 +136,6 @@ components:
- $ref: '#/components/schemas/ClusterProperties'
ClusterId:
type: object
required:
- id
properties:
id:
$ref: '#/components/schemas/Id'
Expand Down
126 changes: 63 additions & 63 deletions pkg/api/openapi.go

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

7 changes: 5 additions & 2 deletions pkg/api/openapi_custom.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package api

// String returns the underlying string value of `Id`.
func (id Id) String() string {
return string(id)
func (id *Id) String() string {
if id == nil {
return ""
}
return string(*id)
}
28 changes: 16 additions & 12 deletions pkg/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ var (
func GenerateClusterID() (ClusterId, error) {
id, err := generateID(ClusterIDPrefix)
return ClusterId{
Id: id,
Id: &id,
}, err
}

// GenerateTenantID creates a new tenant id
func GenerateTenantID() (TenantId, error) {
id, err := generateID(TenantIDPrefix)
return TenantId{
Id: id,
Id: &id,
}, err
}

Expand All @@ -60,8 +60,9 @@ func generateID(prefix string) (Id, error) {

// NewAPITenantFromCRD transforms a CRD tenant into the API representation
func NewAPITenantFromCRD(tenant synv1alpha1.Tenant) *Tenant {
id := Id(tenant.Name)
apiTenant := &Tenant{
TenantId: TenantId{Id: Id(tenant.Name)},
TenantId: TenantId{Id: &id},
TenantProperties: TenantProperties{
GitRepo: &RevisionedGitRepo{},
},
Expand Down Expand Up @@ -107,14 +108,15 @@ func NewAPITenantFromCRD(tenant synv1alpha1.Tenant) *Tenant {
// NewCRDFromAPITenant transforms an API tenant into the CRD representation
func NewCRDFromAPITenant(apiTenant Tenant) (*synv1alpha1.Tenant, error) {
if !strings.HasPrefix(apiTenant.Id.String(), TenantIDPrefix) {
if apiTenant.Id == "" {
if apiTenant.Id.String() == "" {
id, err := GenerateTenantID()
if err != nil {
return nil, err
}
apiTenant.TenantId = id
} else {
apiTenant.Id = TenantIDPrefix + apiTenant.Id
id := TenantIDPrefix + *apiTenant.Id
apiTenant.Id = &id
}
}
if apiTenant.GitRepo == nil ||
Expand All @@ -131,7 +133,7 @@ func NewCRDFromAPITenant(apiTenant Tenant) (*synv1alpha1.Tenant, error) {
}

if apiTenant.GitRepo != nil {
tmpl, err := newGitRepoTemplate(&apiTenant.GitRepo.GitRepo, string(apiTenant.Id))
tmpl, err := newGitRepoTemplate(&apiTenant.GitRepo.GitRepo, apiTenant.Id.String())
if err != nil {
return nil, fmt.Errorf("failed to create git repo template: %w", err)
}
Expand Down Expand Up @@ -179,8 +181,9 @@ func SyncCRDFromAPITenant(source TenantProperties, target *synv1alpha1.Tenant) {

// NewAPIClusterFromCRD transforms a CRD cluster into the API representation
func NewAPIClusterFromCRD(cluster synv1alpha1.Cluster) *Cluster {
id := Id(cluster.Name)
apiCluster := &Cluster{
ClusterId: ClusterId{Id: Id(cluster.Name)},
ClusterId: ClusterId{Id: &id},
ClusterProperties: ClusterProperties{
GitRepo: &GitRepo{},
},
Expand Down Expand Up @@ -257,20 +260,21 @@ func unmarshalFact(fact string) interface{} {

// NewCRDFromAPICluster transforms an API cluster into the CRD representation
func NewCRDFromAPICluster(apiCluster Cluster) (*synv1alpha1.Cluster, error) {
if !strings.HasPrefix(string(apiCluster.Id), ClusterIDPrefix) {
if apiCluster.Id == "" {
if !strings.HasPrefix(apiCluster.Id.String(), ClusterIDPrefix) {
if apiCluster.Id.String() == "" {
id, err := GenerateClusterID()
if err != nil {
return nil, err
}
apiCluster.ClusterId = id
} else {
apiCluster.Id = ClusterIDPrefix + apiCluster.Id
id := Id(ClusterIDPrefix + apiCluster.Id.String())
apiCluster.Id = &id
}
}
cluster := &synv1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: string(apiCluster.ClusterId.Id),
Name: apiCluster.Id.String(),
Annotations: map[string]string{},
},
Spec: synv1alpha1.ClusterSpec{
Expand All @@ -280,7 +284,7 @@ func NewCRDFromAPICluster(apiCluster Cluster) (*synv1alpha1.Cluster, error) {
},
}

tmpl, err := newGitRepoTemplate(apiCluster.GitRepo, string(apiCluster.Id))
tmpl, err := newGitRepoTemplate(apiCluster.GitRepo, apiCluster.Id.String())
if err != nil {
return nil, fmt.Errorf("failed to create git repo template: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestGenerateClusterID(t *testing.T) {
assertGeneratedID(t, ClusterIDPrefix, func() (s string) {
id, err := GenerateClusterID()
require.NoError(t, err)
return string(id.Id)
return id.Id.String()
})
}

Expand Down Expand Up @@ -168,9 +168,10 @@ var tenantTests = map[string]struct {
func TestNewCRDFromAPITenant(t *testing.T) {
for name, test := range tenantTests {
t.Run(name, func(t *testing.T) {
id := Id(fmt.Sprintf("t-%s", t.Name()))
apiTenant := Tenant{
TenantId{
Id: Id(fmt.Sprintf("t-%s", t.Name())),
Id: &id,
},
test.properties,
}
Expand Down Expand Up @@ -234,7 +235,7 @@ func TestNewCRDFromAPICluster(t *testing.T) {
t.Run(name, func(t *testing.T) {
apiCluster := Cluster{
ClusterId{
Id: Id(fmt.Sprintf("c-%s", t.Name())),
Id: pointer.To(Id(fmt.Sprintf("c-%s", t.Name()))),
},
ClusterTenant{fmt.Sprintf("t-%s", t.Name())},
test.properties,
Expand Down
5 changes: 3 additions & 2 deletions pkg/service/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"sort"

"github.com/AlekSi/pointer"
"github.com/labstack/echo/v4"
synv1alpha1 "github.com/projectsyn/lieutenant-operator/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -70,7 +71,7 @@ func sortClustersBy(clusters []api.Cluster, by *api.ListClustersParamsSortBy) {

return di < dj
default:
return clusters[i].Id < clusters[j].Id
return clusters[i].Id.String() < clusters[j].Id.String()
}
})
}
Expand Down Expand Up @@ -188,7 +189,7 @@ func (s *APIImpl) PutCluster(c echo.Context, clusterID api.ClusterIdParameter) e
return echo.NewHTTPError(http.StatusBadRequest, err)
}
apiCluster := api.Cluster(*body)
apiCluster.Id = api.Id(clusterID)
apiCluster.Id = pointer.To(api.Id(clusterID))

cluster, err := api.NewCRDFromAPICluster(apiCluster)
if err != nil {
Expand Down
Loading

0 comments on commit bb052e4

Please sign in to comment.