diff --git a/client/app.go b/client/app.go index 5da634d9c..45333fb3c 100644 --- a/client/app.go +++ b/client/app.go @@ -3,6 +3,9 @@ package client import ( "context" + "github.com/pkg/errors" + "github.com/replicatedhq/replicated/pkg/kotsclient" + "github.com/replicatedhq/replicated/pkg/platformclient" "github.com/replicatedhq/replicated/pkg/types" ) @@ -49,13 +52,45 @@ func (c *Client) ListApps(excludeChannels bool) ([]types.AppAndChannels, error) } func (c *Client) GetApp(appID string) (interface{}, error) { - return nil, nil + app, _, err := c.GetAppType(context.TODO(), appID, true) + if err != nil { + return nil, err + } + return app, nil } func (c *Client) CreateApp(appOptions interface{}) (interface{}, error) { - return nil, nil + switch opts := appOptions.(type) { + case kotsclient.CreateKOTSAppRequest: + return c.KotsClient.CreateKOTSApp(context.TODO(), opts.Name) + case *kotsclient.CreateKOTSAppRequest: + if opts == nil { + return nil, errors.New("create app options cannot be nil") + } + return c.KotsClient.CreateKOTSApp(context.TODO(), opts.Name) + case platformclient.AppOptions: + return c.PlatformClient.CreateApp(&opts) + case *platformclient.AppOptions: + if opts == nil { + return nil, errors.New("create app options cannot be nil") + } + return c.PlatformClient.CreateApp(opts) + default: + return nil, errors.Errorf("unsupported app options type %T", appOptions) + } } func (c *Client) DeleteApp(appID string) error { - return nil + app, appType, err := c.GetAppType(context.TODO(), appID, true) + if err != nil { + return err + } + + if appType == "platform" { + return c.PlatformClient.DeleteApp(app.ID) + } else if appType == "kots" { + return c.KotsClient.DeleteKOTSApp(context.TODO(), app.ID) + } + + return errors.Errorf("unknown app type %q", appType) } diff --git a/client/app_test.go b/client/app_test.go new file mode 100644 index 000000000..126c19b73 --- /dev/null +++ b/client/app_test.go @@ -0,0 +1,47 @@ +package client + +import ( + "testing" + + "github.com/replicatedhq/replicated/pkg/kotsclient" + "github.com/replicatedhq/replicated/pkg/platformclient" + "github.com/stretchr/testify/require" +) + +func newTestClient(origin string) Client { + platformHTTPClient := platformclient.NewHTTPClient(origin, "fake-api-key") + return Client{ + PlatformClient: platformHTTPClient, + KotsClient: &kotsclient.VendorV3Client{HTTPClient: *platformHTTPClient}, + } +} + +func TestClientGetAppReturnsBackendError(t *testing.T) { + c := newTestClient("http://%") + + app, err := c.GetApp("app-id") + require.Nil(t, app) + require.Error(t, err) +} + +func TestClientCreateAppRejectsUnsupportedOptions(t *testing.T) { + c := newTestClient("http://127.0.0.1") + + app, err := c.CreateApp(struct{}{}) + require.Nil(t, app) + require.ErrorContains(t, err, "unsupported app options type") +} + +func TestClientCreateAppRejectsNilOptions(t *testing.T) { + c := newTestClient("http://127.0.0.1") + + app, err := c.CreateApp((*platformclient.AppOptions)(nil)) + require.Nil(t, app) + require.ErrorContains(t, err, "create app options cannot be nil") +} + +func TestClientDeleteAppReturnsLookupError(t *testing.T) { + c := newTestClient("http://%") + + require.Error(t, c.DeleteApp("app-id")) +}