Merge pull request #125 from gabriel-samfira/add-entity-update-subcommand

Add entity update subcommand
This commit is contained in:
Gabriel 2023-07-05 03:06:08 +03:00 committed by GitHub
commit 2efb1b2109
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 193 additions and 37 deletions

View file

@ -136,7 +136,7 @@ func (a *APIController) UpdateEnterpriseHandler(w http.ResponseWriter, r *http.R
return
}
var updatePayload runnerParams.UpdateRepositoryParams
var updatePayload runnerParams.UpdateEntityParams
if err := json.NewDecoder(r.Body).Decode(&updatePayload); err != nil {
handleError(w, gErrors.ErrBadRequest)
return

View file

@ -136,7 +136,7 @@ func (a *APIController) UpdateOrgHandler(w http.ResponseWriter, r *http.Request)
return
}
var updatePayload runnerParams.UpdateRepositoryParams
var updatePayload runnerParams.UpdateEntityParams
if err := json.NewDecoder(r.Body).Decode(&updatePayload); err != nil {
handleError(w, gErrors.ErrBadRequest)
return

View file

@ -184,7 +184,7 @@ func (a *APIController) UpdateRepoHandler(w http.ResponseWriter, r *http.Request
return
}
var updatePayload runnerParams.UpdateRepositoryParams
var updatePayload runnerParams.UpdateEntityParams
if err := json.NewDecoder(r.Body).Decode(&updatePayload); err != nil {
handleError(w, gErrors.ErrBadRequest)
return

View file

@ -51,6 +51,24 @@ func (c *Client) CreateEnterprise(param params.CreateEnterpriseParams) (params.E
return response, nil
}
func (c *Client) UpdateEnterprise(enterpriseID string, param params.UpdateEntityParams) (params.Enterprise, error) {
url := fmt.Sprintf("%s/api/v1/enterprises/%s", c.Config.BaseURL, enterpriseID)
var response params.Enterprise
body, err := json.Marshal(param)
if err != nil {
return response, err
}
resp, err := c.client.R().
SetBody(body).
SetResult(&response).
Put(url)
if err := c.handleError(err, resp); err != nil {
return params.Enterprise{}, err
}
return response, nil
}
func (c *Client) GetEnterprise(enterpriseID string) (params.Enterprise, error) {
var response params.Enterprise
url := fmt.Sprintf("%s/api/v1/enterprises/%s", c.Config.BaseURL, enterpriseID)

View file

@ -51,6 +51,24 @@ func (c *Client) CreateOrganization(param params.CreateOrgParams) (params.Organi
return response, nil
}
func (c *Client) UpdateOrganization(orgID string, param params.UpdateEntityParams) (params.Organization, error) {
url := fmt.Sprintf("%s/api/v1/organizations/%s", c.Config.BaseURL, orgID)
var response params.Organization
body, err := json.Marshal(param)
if err != nil {
return response, err
}
resp, err := c.client.R().
SetBody(body).
SetResult(&response).
Put(url)
if err := c.handleError(err, resp); err != nil {
return params.Organization{}, err
}
return response, nil
}
func (c *Client) GetOrganization(orgID string) (params.Organization, error) {
var response params.Organization
url := fmt.Sprintf("%s/api/v1/organizations/%s", c.Config.BaseURL, orgID)

View file

@ -147,6 +147,24 @@ func (c *Client) UpdateRepoPool(repoID, poolID string, param params.UpdatePoolPa
return response, nil
}
func (c *Client) UpdateRepo(repoID string, param params.UpdateEntityParams) (params.Repository, error) {
url := fmt.Sprintf("%s/api/v1/repositories/%s", c.Config.BaseURL, repoID)
var response params.Repository
body, err := json.Marshal(param)
if err != nil {
return response, err
}
resp, err := c.client.R().
SetBody(body).
SetResult(&response).
Put(url)
if err := c.handleError(err, resp); err != nil {
return params.Repository{}, err
}
return response, nil
}
func (c *Client) ListRepoInstances(repoID string) ([]params.Instance, error) {
url := fmt.Sprintf("%s/api/v1/repositories/%s/instances", c.Config.BaseURL, repoID)

View file

@ -135,6 +135,37 @@ var enterpriseDeleteCmd = &cobra.Command{
},
}
var enterpriseUpdateCmd = &cobra.Command{
Use: "update",
Short: "Update enterprise",
Long: `Update enterprise credentials or webhook secret.`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if needsInit {
return errNeedsInitError
}
if len(args) == 0 {
return fmt.Errorf("command requires a enterprise ID")
}
if len(args) > 1 {
return fmt.Errorf("too many arguments")
}
enterpriseUpdateReq := params.UpdateEntityParams{
WebhookSecret: repoWebhookSecret,
CredentialsName: repoCreds,
}
enterprise, err := cli.UpdateEnterprise(args[0], enterpriseUpdateReq)
if err != nil {
return err
}
formatOneEnterprise(enterprise)
return nil
},
}
func init() {
enterpriseAddCmd.Flags().StringVar(&enterpriseName, "name", "", "The name of the enterprise")
@ -142,12 +173,15 @@ func init() {
enterpriseAddCmd.Flags().StringVar(&enterpriseCreds, "credentials", "", "Credentials name. See credentials list.")
enterpriseAddCmd.MarkFlagRequired("credentials") //nolint
enterpriseAddCmd.MarkFlagRequired("name") //nolint
enterpriseUpdateCmd.Flags().StringVar(&enterpriseWebhookSecret, "webhook-secret", "", "The webhook secret for this enterprise")
enterpriseUpdateCmd.Flags().StringVar(&enterpriseCreds, "credentials", "", "Credentials name. See credentials list.")
enterpriseCmd.AddCommand(
enterpriseListCmd,
enterpriseAddCmd,
enterpriseShowCmd,
enterpriseDeleteCmd,
enterpriseUpdateCmd,
)
rootCmd.AddCommand(enterpriseCmd)

View file

@ -68,6 +68,37 @@ var orgAddCmd = &cobra.Command{
},
}
var orgUpdateCmd = &cobra.Command{
Use: "update",
Short: "Update organization",
Long: `Update organization credentials or webhook secret.`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if needsInit {
return errNeedsInitError
}
if len(args) == 0 {
return fmt.Errorf("command requires a organization ID")
}
if len(args) > 1 {
return fmt.Errorf("too many arguments")
}
orgUpdateReq := params.UpdateEntityParams{
WebhookSecret: repoWebhookSecret,
CredentialsName: orgCreds,
}
org, err := cli.UpdateOrganization(args[0], orgUpdateReq)
if err != nil {
return err
}
formatOneOrganization(org)
return nil
},
}
var orgListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
@ -142,12 +173,15 @@ func init() {
orgAddCmd.Flags().StringVar(&orgCreds, "credentials", "", "Credentials name. See credentials list.")
orgAddCmd.MarkFlagRequired("credentials") //nolint
orgAddCmd.MarkFlagRequired("name") //nolint
orgUpdateCmd.Flags().StringVar(&orgWebhookSecret, "webhook-secret", "", "The webhook secret for this organization")
orgUpdateCmd.Flags().StringVar(&orgCreds, "credentials", "", "Credentials name. See credentials list.")
organizationCmd.AddCommand(
orgListCmd,
orgAddCmd,
orgShowCmd,
orgDeleteCmd,
orgUpdateCmd,
)
rootCmd.AddCommand(organizationCmd)

View file

@ -90,6 +90,37 @@ var repoListCmd = &cobra.Command{
},
}
var repoUpdateCmd = &cobra.Command{
Use: "update",
Short: "Update repository",
Long: `Update repository credentials or webhook secret.`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if needsInit {
return errNeedsInitError
}
if len(args) == 0 {
return fmt.Errorf("command requires a repo ID")
}
if len(args) > 1 {
return fmt.Errorf("too many arguments")
}
repoUpdateReq := params.UpdateEntityParams{
WebhookSecret: repoWebhookSecret,
CredentialsName: repoCreds,
}
repo, err := cli.UpdateRepo(args[0], repoUpdateReq)
if err != nil {
return err
}
formatOneRepository(repo)
return nil
},
}
var repoShowCmd = &cobra.Command{
Use: "show",
Short: "Show details for one repository",
@ -146,12 +177,15 @@ func init() {
repoAddCmd.MarkFlagRequired("credentials") //nolint
repoAddCmd.MarkFlagRequired("owner") //nolint
repoAddCmd.MarkFlagRequired("name") //nolint
repoUpdateCmd.Flags().StringVar(&repoWebhookSecret, "webhook-secret", "", "The webhook secret for this repository")
repoUpdateCmd.Flags().StringVar(&repoCreds, "credentials", "", "Credentials name. See credentials list.")
repositoryCmd.AddCommand(
repoListCmd,
repoAddCmd,
repoShowCmd,
repoDeleteCmd,
repoUpdateCmd,
)
rootCmd.AddCommand(repositoryCmd)

View file

@ -26,7 +26,7 @@ type RepoStore interface {
GetRepositoryByID(ctx context.Context, repoID string) (params.Repository, error)
ListRepositories(ctx context.Context) ([]params.Repository, error)
DeleteRepository(ctx context.Context, repoID string) error
UpdateRepository(ctx context.Context, repoID string, param params.UpdateRepositoryParams) (params.Repository, error)
UpdateRepository(ctx context.Context, repoID string, param params.UpdateEntityParams) (params.Repository, error)
CreateRepositoryPool(ctx context.Context, repoId string, param params.CreatePoolParams) (params.Pool, error)
@ -45,7 +45,7 @@ type OrgStore interface {
GetOrganizationByID(ctx context.Context, orgID string) (params.Organization, error)
ListOrganizations(ctx context.Context) ([]params.Organization, error)
DeleteOrganization(ctx context.Context, orgID string) error
UpdateOrganization(ctx context.Context, orgID string, param params.UpdateRepositoryParams) (params.Organization, error)
UpdateOrganization(ctx context.Context, orgID string, param params.UpdateEntityParams) (params.Organization, error)
CreateOrganizationPool(ctx context.Context, orgId string, param params.CreatePoolParams) (params.Pool, error)
GetOrganizationPool(ctx context.Context, orgID, poolID string) (params.Pool, error)
@ -63,7 +63,7 @@ type EnterpriseStore interface {
GetEnterpriseByID(ctx context.Context, enterpriseID string) (params.Enterprise, error)
ListEnterprises(ctx context.Context) ([]params.Enterprise, error)
DeleteEnterprise(ctx context.Context, enterpriseID string) error
UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateRepositoryParams) (params.Enterprise, error)
UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateEntityParams) (params.Enterprise, error)
CreateEnterprisePool(ctx context.Context, enterpriseID string, param params.CreatePoolParams) (params.Pool, error)
GetEnterprisePool(ctx context.Context, enterpriseID, poolID string) (params.Pool, error)

View file

@ -1165,21 +1165,21 @@ func (_m *Store) PoolInstanceCount(ctx context.Context, poolID string) (int64, e
}
// UpdateEnterprise provides a mock function with given fields: ctx, enterpriseID, param
func (_m *Store) UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateRepositoryParams) (params.Enterprise, error) {
func (_m *Store) UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateEntityParams) (params.Enterprise, error) {
ret := _m.Called(ctx, enterpriseID, param)
var r0 params.Enterprise
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateRepositoryParams) (params.Enterprise, error)); ok {
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateEntityParams) (params.Enterprise, error)); ok {
return rf(ctx, enterpriseID, param)
}
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateRepositoryParams) params.Enterprise); ok {
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateEntityParams) params.Enterprise); ok {
r0 = rf(ctx, enterpriseID, param)
} else {
r0 = ret.Get(0).(params.Enterprise)
}
if rf, ok := ret.Get(1).(func(context.Context, string, params.UpdateRepositoryParams) error); ok {
if rf, ok := ret.Get(1).(func(context.Context, string, params.UpdateEntityParams) error); ok {
r1 = rf(ctx, enterpriseID, param)
} else {
r1 = ret.Error(1)
@ -1237,21 +1237,21 @@ func (_m *Store) UpdateInstance(ctx context.Context, instanceID string, param pa
}
// UpdateOrganization provides a mock function with given fields: ctx, orgID, param
func (_m *Store) UpdateOrganization(ctx context.Context, orgID string, param params.UpdateRepositoryParams) (params.Organization, error) {
func (_m *Store) UpdateOrganization(ctx context.Context, orgID string, param params.UpdateEntityParams) (params.Organization, error) {
ret := _m.Called(ctx, orgID, param)
var r0 params.Organization
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateRepositoryParams) (params.Organization, error)); ok {
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateEntityParams) (params.Organization, error)); ok {
return rf(ctx, orgID, param)
}
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateRepositoryParams) params.Organization); ok {
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateEntityParams) params.Organization); ok {
r0 = rf(ctx, orgID, param)
} else {
r0 = ret.Get(0).(params.Organization)
}
if rf, ok := ret.Get(1).(func(context.Context, string, params.UpdateRepositoryParams) error); ok {
if rf, ok := ret.Get(1).(func(context.Context, string, params.UpdateEntityParams) error); ok {
r1 = rf(ctx, orgID, param)
} else {
r1 = ret.Error(1)
@ -1285,21 +1285,21 @@ func (_m *Store) UpdateOrganizationPool(ctx context.Context, orgID string, poolI
}
// UpdateRepository provides a mock function with given fields: ctx, repoID, param
func (_m *Store) UpdateRepository(ctx context.Context, repoID string, param params.UpdateRepositoryParams) (params.Repository, error) {
func (_m *Store) UpdateRepository(ctx context.Context, repoID string, param params.UpdateEntityParams) (params.Repository, error) {
ret := _m.Called(ctx, repoID, param)
var r0 params.Repository
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateRepositoryParams) (params.Repository, error)); ok {
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateEntityParams) (params.Repository, error)); ok {
return rf(ctx, repoID, param)
}
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateRepositoryParams) params.Repository); ok {
if rf, ok := ret.Get(0).(func(context.Context, string, params.UpdateEntityParams) params.Repository); ok {
r0 = rf(ctx, repoID, param)
} else {
r0 = ret.Get(0).(params.Repository)
}
if rf, ok := ret.Get(1).(func(context.Context, string, params.UpdateRepositoryParams) error); ok {
if rf, ok := ret.Get(1).(func(context.Context, string, params.UpdateEntityParams) error); ok {
r1 = rf(ctx, repoID, param)
} else {
r1 = ret.Error(1)

View file

@ -99,7 +99,7 @@ func (s *sqlDatabase) DeleteEnterprise(ctx context.Context, enterpriseID string)
return nil
}
func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateRepositoryParams) (params.Enterprise, error) {
func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateEntityParams) (params.Enterprise, error) {
enterprise, err := s.getEnterpriseByID(ctx, enterpriseID)
if err != nil {
return params.Enterprise{}, errors.Wrap(err, "fetching enterprise")

View file

@ -40,7 +40,7 @@ type EnterpriseTestFixtures struct {
CreateEnterpriseParams params.CreateEnterpriseParams
CreatePoolParams params.CreatePoolParams
CreateInstanceParams params.CreateInstanceParams
UpdateRepoParams params.UpdateRepositoryParams
UpdateRepoParams params.UpdateEntityParams
UpdatePoolParams params.UpdatePoolParams
SQLMock sqlmock.Sqlmock
}
@ -142,7 +142,7 @@ func (s *EnterpriseTestSuite) SetupTest() {
Name: "test-instance-name",
OSType: "linux",
},
UpdateRepoParams: params.UpdateRepositoryParams{
UpdateRepoParams: params.UpdateEntityParams{
CredentialsName: "test-update-creds",
WebhookSecret: "test-update-repo-webhook-secret",
},

View file

@ -103,7 +103,7 @@ func (s *sqlDatabase) DeleteOrganization(ctx context.Context, orgID string) erro
return nil
}
func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, param params.UpdateRepositoryParams) (params.Organization, error) {
func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, param params.UpdateEntityParams) (params.Organization, error) {
org, err := s.getOrgByID(ctx, orgID)
if err != nil {
return params.Organization{}, errors.Wrap(err, "fetching org")

View file

@ -39,7 +39,7 @@ type OrgTestFixtures struct {
CreateOrgParams params.CreateOrgParams
CreatePoolParams params.CreatePoolParams
CreateInstanceParams params.CreateInstanceParams
UpdateRepoParams params.UpdateRepositoryParams
UpdateRepoParams params.UpdateEntityParams
UpdatePoolParams params.UpdatePoolParams
SQLMock sqlmock.Sqlmock
}
@ -141,7 +141,7 @@ func (s *OrgTestSuite) SetupTest() {
Name: "test-instance-name",
OSType: "linux",
},
UpdateRepoParams: params.UpdateRepositoryParams{
UpdateRepoParams: params.UpdateEntityParams{
CredentialsName: "test-update-creds",
WebhookSecret: "test-update-repo-webhook-secret",
},

View file

@ -103,7 +103,7 @@ func (s *sqlDatabase) DeleteRepository(ctx context.Context, repoID string) error
return nil
}
func (s *sqlDatabase) UpdateRepository(ctx context.Context, repoID string, param params.UpdateRepositoryParams) (params.Repository, error) {
func (s *sqlDatabase) UpdateRepository(ctx context.Context, repoID string, param params.UpdateEntityParams) (params.Repository, error) {
repo, err := s.getRepoByID(ctx, repoID)
if err != nil {
return params.Repository{}, errors.Wrap(err, "fetching repo")

View file

@ -38,7 +38,7 @@ type RepoTestFixtures struct {
CreateRepoParams params.CreateRepoParams
CreatePoolParams params.CreatePoolParams
CreateInstanceParams params.CreateInstanceParams
UpdateRepoParams params.UpdateRepositoryParams
UpdateRepoParams params.UpdateEntityParams
UpdatePoolParams params.UpdatePoolParams
SQLMock sqlmock.Sqlmock
}
@ -153,7 +153,7 @@ func (s *RepoTestSuite) SetupTest() {
Name: "test-instance",
OSType: "linux",
},
UpdateRepoParams: params.UpdateRepositoryParams{
UpdateRepoParams: params.UpdateEntityParams{
CredentialsName: "test-update-creds",
WebhookSecret: "test-update-webhook-secret",
},

View file

@ -227,7 +227,7 @@ func (p PasswordLoginParams) Validate() error {
return nil
}
type UpdateRepositoryParams struct {
type UpdateEntityParams struct {
CredentialsName string `json:"credentials_name"`
WebhookSecret string `json:"webhook_secret"`
}

View file

@ -144,7 +144,7 @@ func (r *Runner) DeleteEnterprise(ctx context.Context, enterpriseID string) erro
return nil
}
func (r *Runner) UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateRepositoryParams) (params.Enterprise, error) {
func (r *Runner) UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateEntityParams) (params.Enterprise, error) {
if !auth.IsAdmin(ctx) {
return params.Enterprise{}, runnerErrors.ErrUnauthorized
}

View file

@ -44,7 +44,7 @@ type EnterpriseTestFixtures struct {
CreateEnterpriseParams params.CreateEnterpriseParams
CreatePoolParams params.CreatePoolParams
CreateInstanceParams params.CreateInstanceParams
UpdateRepoParams params.UpdateRepositoryParams
UpdateRepoParams params.UpdateEntityParams
UpdatePoolParams params.UpdatePoolParams
UpdatePoolStateParams params.UpdatePoolStateParams
ErrMock error
@ -124,7 +124,7 @@ func (s *EnterpriseTestSuite) SetupTest() {
Name: "test-instance-name",
OSType: "linux",
},
UpdateRepoParams: params.UpdateRepositoryParams{
UpdateRepoParams: params.UpdateEntityParams{
CredentialsName: "test-creds",
WebhookSecret: "test-update-repo-webhook-secret",
},

View file

@ -158,7 +158,7 @@ func (r *Runner) DeleteOrganization(ctx context.Context, orgID string) error {
return nil
}
func (r *Runner) UpdateOrganization(ctx context.Context, orgID string, param params.UpdateRepositoryParams) (params.Organization, error) {
func (r *Runner) UpdateOrganization(ctx context.Context, orgID string, param params.UpdateEntityParams) (params.Organization, error) {
if !auth.IsAdmin(ctx) {
return params.Organization{}, runnerErrors.ErrUnauthorized
}

View file

@ -44,7 +44,7 @@ type OrgTestFixtures struct {
CreateOrgParams params.CreateOrgParams
CreatePoolParams params.CreatePoolParams
CreateInstanceParams params.CreateInstanceParams
UpdateRepoParams params.UpdateRepositoryParams
UpdateRepoParams params.UpdateEntityParams
UpdatePoolParams params.UpdatePoolParams
UpdatePoolStateParams params.UpdatePoolStateParams
ErrMock error
@ -124,7 +124,7 @@ func (s *OrgTestSuite) SetupTest() {
Name: "test-instance-name",
OSType: "linux",
},
UpdateRepoParams: params.UpdateRepositoryParams{
UpdateRepoParams: params.UpdateEntityParams{
CredentialsName: "test-creds",
WebhookSecret: "test-update-repo-webhook-secret",
},

View file

@ -157,7 +157,7 @@ func (r *Runner) DeleteRepository(ctx context.Context, repoID string) error {
return nil
}
func (r *Runner) UpdateRepository(ctx context.Context, repoID string, param params.UpdateRepositoryParams) (params.Repository, error) {
func (r *Runner) UpdateRepository(ctx context.Context, repoID string, param params.UpdateEntityParams) (params.Repository, error) {
if !auth.IsAdmin(ctx) {
return params.Repository{}, runnerErrors.ErrUnauthorized
}

View file

@ -43,7 +43,7 @@ type RepoTestFixtures struct {
CreateRepoParams params.CreateRepoParams
CreatePoolParams params.CreatePoolParams
CreateInstanceParams params.CreateInstanceParams
UpdateRepoParams params.UpdateRepositoryParams
UpdateRepoParams params.UpdateEntityParams
UpdatePoolParams params.UpdatePoolParams
UpdatePoolStateParams params.UpdatePoolStateParams
ErrMock error
@ -124,7 +124,7 @@ func (s *RepoTestSuite) SetupTest() {
Name: "test-instance-name",
OSType: "linux",
},
UpdateRepoParams: params.UpdateRepositoryParams{
UpdateRepoParams: params.UpdateEntityParams{
CredentialsName: "test-creds",
WebhookSecret: "test-update-repo-webhook-secret",
},