-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from ninech/project-display-name
Add project display name support
- Loading branch information
Showing
7 changed files
with
173 additions
and
19 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
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
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
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
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,53 @@ | ||
package update | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/crossplane/crossplane-runtime/pkg/resource" | ||
management "github.com/ninech/apis/management/v1alpha1" | ||
"github.com/ninech/nctl/api" | ||
"github.com/ninech/nctl/auth" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type projectCmd struct { | ||
Name string `arg:"" default:"" help:"Name of the project to update."` | ||
DisplayName *string `default:"" help:"Display Name of the project."` | ||
} | ||
|
||
func (cmd *projectCmd) Run(ctx context.Context, client *api.Client) error { | ||
cfg, err := auth.ReadConfig(client.KubeconfigPath, client.KubeconfigContext) | ||
if err != nil { | ||
if auth.IsConfigNotFoundError(err) { | ||
return auth.ReloginNeeded(err) | ||
} | ||
return err | ||
} | ||
|
||
project := &management.Project{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: cmd.Name, | ||
Namespace: cfg.Organization, | ||
}, | ||
} | ||
|
||
upd := newUpdater(client, project, management.ProjectKind, func(current resource.Managed) error { | ||
project, ok := current.(*management.Project) | ||
if !ok { | ||
return fmt.Errorf("resource is of type %T, expected %T", current, management.Project{}) | ||
} | ||
|
||
cmd.applyUpdates(project) | ||
|
||
return nil | ||
}) | ||
|
||
return upd.Update(ctx) | ||
} | ||
|
||
func (cmd *projectCmd) applyUpdates(project *management.Project) { | ||
if cmd.DisplayName != nil { | ||
project.Spec.DisplayName = *cmd.DisplayName | ||
} | ||
} |
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,81 @@ | ||
package update | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
management "github.com/ninech/apis/management/v1alpha1" | ||
"github.com/ninech/nctl/api" | ||
"github.com/ninech/nctl/internal/test" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
func TestProject(t *testing.T) { | ||
const ( | ||
projectName = "some-project" | ||
organization = "org" | ||
) | ||
existingProject := &management.Project{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: projectName, | ||
Namespace: organization, | ||
}, | ||
Spec: management.ProjectSpec{}, | ||
} | ||
|
||
cases := map[string]struct { | ||
orig *management.Project | ||
project string | ||
cmd projectCmd | ||
checkProject func(t *testing.T, cmd projectCmd, orig, updated *management.Project) | ||
}{ | ||
"all fields update": { | ||
orig: existingProject, | ||
project: projectName, | ||
cmd: projectCmd{ | ||
Name: projectName, | ||
DisplayName: ptr.To("some display name"), | ||
}, | ||
checkProject: func(t *testing.T, cmd projectCmd, orig, updated *management.Project) { | ||
assert.Equal(t, *cmd.DisplayName, updated.Spec.DisplayName) | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
tc := tc | ||
|
||
t.Run(name, func(t *testing.T) { | ||
apiClient, err := test.SetupClient(tc.orig) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
apiClient.Project = tc.project | ||
|
||
ctx := context.Background() | ||
|
||
// we create a kubeconfig which does not contain a nctl config | ||
// extension | ||
kubeconfig, err := test.CreateTestKubeconfig(apiClient, organization) | ||
require.NoError(t, err) | ||
defer os.Remove(kubeconfig) | ||
|
||
if err := tc.cmd.Run(ctx, apiClient); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
updated := &management.Project{} | ||
if err := apiClient.Get(ctx, api.ObjectName(tc.orig), updated); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if tc.checkProject != nil { | ||
tc.checkProject(t, tc.cmd, tc.orig, updated) | ||
} | ||
}) | ||
} | ||
} |
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