diff --git a/internal/services/gateway/command/command.go b/internal/services/gateway/action/action.go similarity index 87% rename from internal/services/gateway/command/command.go rename to internal/services/gateway/action/action.go index 9a10ceb..57d5f74 100644 --- a/internal/services/gateway/command/command.go +++ b/internal/services/gateway/action/action.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package command +package action import ( "net/http" @@ -25,7 +25,7 @@ import ( "go.uber.org/zap" ) -type CommandHandler struct { +type ActionHandler struct { log *zap.SugaredLogger sd *common.TokenSigningData configstoreClient *csapi.Client @@ -34,8 +34,8 @@ type CommandHandler struct { webExposedURL string } -func NewCommandHandler(logger *zap.Logger, sd *common.TokenSigningData, configstoreClient *csapi.Client, agolaID, apiExposedURL, webExposedURL string) *CommandHandler { - return &CommandHandler{ +func NewActionHandler(logger *zap.Logger, sd *common.TokenSigningData, configstoreClient *csapi.Client, agolaID, apiExposedURL, webExposedURL string) *ActionHandler { + return &ActionHandler{ log: logger.Sugar(), sd: sd, configstoreClient: configstoreClient, diff --git a/internal/services/gateway/command/org.go b/internal/services/gateway/action/org.go similarity index 81% rename from internal/services/gateway/command/org.go rename to internal/services/gateway/action/org.go index 4c0d31f..dd7319d 100644 --- a/internal/services/gateway/command/org.go +++ b/internal/services/gateway/action/org.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package command +package action import ( "context" @@ -29,7 +29,7 @@ type CreateOrgRequest struct { CreatorUserID string } -func (c *CommandHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*types.Organization, error) { +func (h *ActionHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*types.Organization, error) { if req.Name == "" { return nil, util.NewErrBadRequest(errors.Errorf("organization name required")) } @@ -44,12 +44,12 @@ func (c *CommandHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) ( org.CreatorUserID = req.CreatorUserID } - c.log.Infof("creating organization") - org, resp, err := c.configstoreClient.CreateOrg(ctx, org) + h.log.Infof("creating organization") + org, resp, err := h.configstoreClient.CreateOrg(ctx, org) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create organization")) } - c.log.Infof("organization %s created, ID: %s", org.Name, org.ID) + h.log.Infof("organization %s created, ID: %s", org.Name, org.ID) return org, nil } diff --git a/internal/services/gateway/command/project.go b/internal/services/gateway/action/project.go similarity index 77% rename from internal/services/gateway/command/project.go rename to internal/services/gateway/action/project.go index b41e3e6..c749ded 100644 --- a/internal/services/gateway/command/project.go +++ b/internal/services/gateway/action/project.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package command +package action import ( "context" @@ -38,7 +38,7 @@ type CreateProjectRequest struct { SkipSSHHostKeyCheck bool } -func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRequest) (*csapi.Project, error) { +func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectRequest) (*csapi.Project, error) { if !util.ValidateName(req.Name) { return nil, util.NewErrBadRequest(errors.Errorf("invalid project name %q", req.Name)) } @@ -49,13 +49,13 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe return nil, util.NewErrBadRequest(errors.Errorf("empty remote repo path")) } - pg, resp, err := c.configstoreClient.GetProjectGroup(ctx, req.ParentID) + pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, req.ParentID) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project group %q", req.Name)) } projectPath := path.Join(pg.Path, req.Name) - _, resp, err = c.configstoreClient.GetProject(ctx, projectPath) + _, resp, err = h.configstoreClient.GetProject(ctx, projectPath) if err != nil { if resp != nil && resp.StatusCode != http.StatusNotFound { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", req.Name)) @@ -64,16 +64,16 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe return nil, util.NewErrBadRequest(errors.Errorf("project %q already exists", projectPath)) } - user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID) + user, resp, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID)) } - rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) + rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) } - c.log.Infof("rs: %s", util.Dump(rs)) + h.log.Infof("rs: %s", util.Dump(rs)) var la *types.LinkedAccount for _, v := range user.LinkedAccounts { if v.RemoteSourceID == rs.ID { @@ -81,12 +81,12 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe break } } - c.log.Infof("la: %s", util.Dump(la)) + h.log.Infof("la: %s", util.Dump(la)) if la == nil { return nil, errors.Errorf("user doesn't have a linked account for remote source %q", rs.Name) } - gitsource, err := c.GetGitSource(ctx, rs, user.Name, la) + gitsource, err := h.GetGitSource(ctx, rs, user.Name, la) if err != nil { return nil, errors.Wrapf(err, "failed to create gitsource client") } @@ -96,7 +96,7 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe return nil, errors.Wrapf(err, "failed to get repository info from gitsource") } - c.log.Infof("generating ssh key pairs") + h.log.Infof("generating ssh key pairs") privateKey, _, err := util.GenSSHKeyPair(4096) if err != nil { return nil, errors.Wrapf(err, "failed to generate ssh key pair") @@ -124,18 +124,18 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe SSHPrivateKey: string(privateKey), } - c.log.Infof("creating project") - rp, resp, err := c.configstoreClient.CreateProject(ctx, p) + h.log.Infof("creating project") + rp, resp, err := h.configstoreClient.CreateProject(ctx, p) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create project")) } - c.log.Infof("project %s created, ID: %s", p.Name, p.ID) + h.log.Infof("project %s created, ID: %s", p.Name, p.ID) - return rp, c.SetupProject(ctx, rs, user, la, rp) + return rp, h.SetupProject(ctx, rs, user, la, rp) } -func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, user *types.User, la *types.LinkedAccount, project *csapi.Project) error { - gitsource, err := c.GetGitSource(ctx, rs, user.Name, la) +func (h *ActionHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, user *types.User, la *types.LinkedAccount, project *csapi.Project) error { + gitsource, err := h.GetGitSource(ctx, rs, user.Name, la) if err != nil { return errors.Wrapf(err, "failed to create gitsource client") } @@ -145,28 +145,28 @@ func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSourc return errors.Wrapf(err, "failed to extract public key") } - webhookURL, err := url.Parse(fmt.Sprintf("%s/webhooks", c.apiExposedURL)) + webhookURL, err := url.Parse(fmt.Sprintf("%s/webhooks", h.apiExposedURL)) if err != nil { return errors.Wrapf(err, "failed to generate webhook url") } q := url.Values{} q.Add("projectid", project.ID) - q.Add("agolaid", c.agolaID) + q.Add("agolaid", h.agolaID) webhookURL.RawQuery = q.Encode() // generate deploy keys and webhooks containing the agola project id so we // can have multiple projects referencing the same remote repository and this // will trigger multiple different runs deployKeyName := fmt.Sprintf("agola deploy key - %s", project.ID) - c.log.Infof("creating/updating deploy key: %s", string(pubKey)) + h.log.Infof("creating/updating deploy key: %s", string(pubKey)) if err := gitsource.UpdateDeployKey(project.RepositoryPath, deployKeyName, string(pubKey), true); err != nil { return errors.Wrapf(err, "failed to create deploy key") } - c.log.Infof("deleting existing webhooks") + h.log.Infof("deleting existing webhooks") if err := gitsource.DeleteRepoWebhook(project.RepositoryPath, webhookURL.String()); err != nil { return errors.Wrapf(err, "failed to delete repository webhook") } - c.log.Infof("creating webhook to url: %s", webhookURL) + h.log.Infof("creating webhook to url: %s", webhookURL) if err := gitsource.CreateRepoWebhook(project.RepositoryPath, webhookURL.String(), ""); err != nil { return errors.Wrapf(err, "failed to create repository webhook") } @@ -174,29 +174,29 @@ func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSourc return nil } -func (c *CommandHandler) ReconfigProject(ctx context.Context, projectRef string) error { - p, resp, err := c.configstoreClient.GetProject(ctx, projectRef) +func (h *ActionHandler) ReconfigProject(ctx context.Context, projectRef string) error { + p, resp, err := h.configstoreClient.GetProject(ctx, projectRef) if err != nil { return ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", projectRef)) } - user, resp, err := c.configstoreClient.GetUserByLinkedAccount(ctx, p.LinkedAccountID) + user, resp, err := h.configstoreClient.GetUserByLinkedAccount(ctx, p.LinkedAccountID) if err != nil { return ErrFromRemote(resp, errors.Wrapf(err, "failed to get user with linked account id %q", p.LinkedAccountID)) } la := user.LinkedAccounts[p.LinkedAccountID] - c.log.Infof("la: %s", util.Dump(la)) + h.log.Infof("la: %s", util.Dump(la)) if la == nil { return errors.Errorf("linked account %q in user %q doesn't exist", p.LinkedAccountID, user.Name) } - rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID) + rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID) if err != nil { return ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID)) } // TODO(sgotti) update project repo path if the remote let us query by repository id - return c.SetupProject(ctx, rs, user, la, p) + return h.SetupProject(ctx, rs, user, la, p) } diff --git a/internal/services/gateway/command/projectgroup.go b/internal/services/gateway/action/projectgroup.go similarity index 81% rename from internal/services/gateway/command/projectgroup.go rename to internal/services/gateway/action/projectgroup.go index 3875252..a35529d 100644 --- a/internal/services/gateway/command/projectgroup.go +++ b/internal/services/gateway/action/projectgroup.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package command +package action import ( "context" @@ -32,12 +32,12 @@ type CreateProjectGroupRequest struct { Visibility types.Visibility } -func (c *CommandHandler) CreateProjectGroup(ctx context.Context, req *CreateProjectGroupRequest) (*csapi.ProjectGroup, error) { +func (h *ActionHandler) CreateProjectGroup(ctx context.Context, req *CreateProjectGroupRequest) (*csapi.ProjectGroup, error) { if !util.ValidateName(req.Name) { return nil, util.NewErrBadRequest(errors.Errorf("invalid projectGroup name %q", req.Name)) } - user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID) + user, resp, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID)) } @@ -57,12 +57,12 @@ func (c *CommandHandler) CreateProjectGroup(ctx context.Context, req *CreateProj Visibility: req.Visibility, } - c.log.Infof("creating projectGroup") - rp, resp, err := c.configstoreClient.CreateProjectGroup(ctx, p) + h.log.Infof("creating projectGroup") + rp, resp, err := h.configstoreClient.CreateProjectGroup(ctx, p) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create projectGroup")) } - c.log.Infof("projectGroup %s created, ID: %s", rp.Name, rp.ID) + h.log.Infof("projectGroup %s created, ID: %s", rp.Name, rp.ID) return rp, nil } diff --git a/internal/services/gateway/command/remotesource.go b/internal/services/gateway/action/remotesource.go similarity index 89% rename from internal/services/gateway/command/remotesource.go rename to internal/services/gateway/action/remotesource.go index 2d86466..251e520 100644 --- a/internal/services/gateway/command/remotesource.go +++ b/internal/services/gateway/action/remotesource.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package command +package action import ( "context" @@ -32,7 +32,7 @@ type CreateRemoteSourceRequest struct { Oauth2ClientSecret string } -func (c *CommandHandler) CreateRemoteSource(ctx context.Context, req *CreateRemoteSourceRequest) (*types.RemoteSource, error) { +func (h *ActionHandler) CreateRemoteSource(ctx context.Context, req *CreateRemoteSourceRequest) (*types.RemoteSource, error) { if !util.ValidateName(req.Name) { return nil, util.NewErrBadRequest(errors.Errorf("invalid remotesource name %q", req.Name)) } @@ -73,12 +73,12 @@ func (c *CommandHandler) CreateRemoteSource(ctx context.Context, req *CreateRemo Oauth2ClientSecret: req.Oauth2ClientSecret, } - c.log.Infof("creating remotesource") - rs, resp, err := c.configstoreClient.CreateRemoteSource(ctx, rs) + h.log.Infof("creating remotesource") + rs, resp, err := h.configstoreClient.CreateRemoteSource(ctx, rs) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create remotesource")) } - c.log.Infof("remotesource %s created, ID: %s", rs.Name, rs.ID) + h.log.Infof("remotesource %s created, ID: %s", rs.Name, rs.ID) return rs, nil } diff --git a/internal/services/gateway/command/user.go b/internal/services/gateway/action/user.go similarity index 79% rename from internal/services/gateway/command/user.go rename to internal/services/gateway/action/user.go index ad6de70..bf7e882 100644 --- a/internal/services/gateway/command/user.go +++ b/internal/services/gateway/action/user.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package command +package action import ( "context" @@ -44,7 +44,7 @@ type CreateUserRequest struct { UserName string } -func (c *CommandHandler) CreateUser(ctx context.Context, req *CreateUserRequest) (*types.User, error) { +func (h *ActionHandler) CreateUser(ctx context.Context, req *CreateUserRequest) (*types.User, error) { if req.UserName == "" { return nil, util.NewErrBadRequest(errors.Errorf("user name required")) } @@ -56,12 +56,12 @@ func (c *CommandHandler) CreateUser(ctx context.Context, req *CreateUserRequest) UserName: req.UserName, } - c.log.Infof("creating user") - u, resp, err := c.configstoreClient.CreateUser(ctx, creq) + h.log.Infof("creating user") + u, resp, err := h.configstoreClient.CreateUser(ctx, creq) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create user")) } - c.log.Infof("user %s created, ID: %s", u.Name, u.ID) + h.log.Infof("user %s created, ID: %s", u.Name, u.ID) return u, nil } @@ -71,7 +71,7 @@ type CreateUserTokenRequest struct { TokenName string } -func (c *CommandHandler) CreateUserToken(ctx context.Context, req *CreateUserTokenRequest) (string, error) { +func (h *ActionHandler) CreateUserToken(ctx context.Context, req *CreateUserTokenRequest) (string, error) { var userID string userIDVal := ctx.Value("userid") if userIDVal != nil { @@ -85,7 +85,7 @@ func (c *CommandHandler) CreateUserToken(ctx context.Context, req *CreateUserTok } userRef := req.UserRef - user, resp, err := c.configstoreClient.GetUser(ctx, userRef) + user, resp, err := h.configstoreClient.GetUser(ctx, userRef) if err != nil { return "", ErrFromRemote(resp, errors.Wrapf(err, "failed to get user")) } @@ -98,15 +98,15 @@ func (c *CommandHandler) CreateUserToken(ctx context.Context, req *CreateUserTok return "", util.NewErrBadRequest(errors.Errorf("user %q already have a token with name %q", userRef, req.TokenName)) } - c.log.Infof("creating user token") + h.log.Infof("creating user token") creq := &csapi.CreateUserTokenRequest{ TokenName: req.TokenName, } - res, resp, err := c.configstoreClient.CreateUserToken(ctx, userRef, creq) + res, resp, err := h.configstoreClient.CreateUserToken(ctx, userRef, creq) if err != nil { return "", ErrFromRemote(resp, errors.Wrapf(err, "failed to create user token")) } - c.log.Infof("token %q for user %q created", req.TokenName, userRef) + h.log.Infof("token %q for user %q created", req.TokenName, userRef) return res.Token, nil } @@ -121,17 +121,17 @@ type CreateUserLARequest struct { Oauth2AccessTokenExpiresAt time.Time } -func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequest) (*types.LinkedAccount, error) { +func (h *ActionHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequest) (*types.LinkedAccount, error) { userRef := req.UserRef - user, resp, err := c.configstoreClient.GetUser(ctx, userRef) + user, resp, err := h.configstoreClient.GetUser(ctx, userRef) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", userRef)) } - rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) + rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) } - c.log.Infof("rs: %s", util.Dump(rs)) + h.log.Infof("rs: %s", util.Dump(rs)) var la *types.LinkedAccount for _, v := range user.LinkedAccounts { if v.RemoteSourceID == rs.ID { @@ -139,7 +139,7 @@ func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequ break } } - c.log.Infof("la: %s", util.Dump(la)) + h.log.Infof("la: %s", util.Dump(la)) if la != nil { return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", userRef, rs.Name)) } @@ -171,18 +171,18 @@ func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequ Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt, } - c.log.Infof("creating linked account") - la, resp, err = c.configstoreClient.CreateUserLA(ctx, userRef, creq) + h.log.Infof("creating linked account") + la, resp, err = h.configstoreClient.CreateUserLA(ctx, userRef, creq) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create linked account")) } - c.log.Infof("linked account %q for user %q created", la.ID, userRef) + h.log.Infof("linked account %q for user %q created", la.ID, userRef) return la, nil } -func (c *CommandHandler) UpdateUserLA(ctx context.Context, userRef string, la *types.LinkedAccount) error { - user, resp, err := c.configstoreClient.GetUser(ctx, userRef) +func (h *ActionHandler) UpdateUserLA(ctx context.Context, userRef string, la *types.LinkedAccount) error { + user, resp, err := h.configstoreClient.GetUser(ctx, userRef) if err != nil { return ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", userRef)) } @@ -193,7 +193,7 @@ func (c *CommandHandler) UpdateUserLA(ctx context.Context, userRef string, la *t break } } - c.log.Infof("la: %s", util.Dump(la)) + h.log.Infof("la: %s", util.Dump(la)) if !laFound { return util.NewErrBadRequest(errors.Errorf("user %q doesn't have a linked account with id %q", userRef, la.ID)) } @@ -207,18 +207,18 @@ func (c *CommandHandler) UpdateUserLA(ctx context.Context, userRef string, la *t Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt, } - c.log.Infof("updating user %q linked account", userRef) - la, resp, err = c.configstoreClient.UpdateUserLA(ctx, userRef, la.ID, creq) + h.log.Infof("updating user %q linked account", userRef) + la, resp, err = h.configstoreClient.UpdateUserLA(ctx, userRef, la.ID, creq) if err != nil { return ErrFromRemote(resp, errors.Wrapf(err, "failed to update user")) } - c.log.Infof("linked account %q for user %q updated", la.ID, userRef) + h.log.Infof("linked account %q for user %q updated", la.ID, userRef) return nil } // RefreshLinkedAccount refreshed the linked account oauth2 access token and update linked account in the configstore -func (c *CommandHandler) RefreshLinkedAccount(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (*types.LinkedAccount, error) { +func (h *ActionHandler) RefreshLinkedAccount(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (*types.LinkedAccount, error) { switch rs.AuthType { case types.RemoteSourceAuthTypeOauth2: // refresh access token if expired @@ -237,7 +237,7 @@ func (c *CommandHandler) RefreshLinkedAccount(ctx context.Context, rs *types.Rem la.Oauth2RefreshToken = token.RefreshToken la.Oauth2AccessTokenExpiresAt = token.Expiry - if err := c.UpdateUserLA(ctx, userName, la); err != nil { + if err := h.UpdateUserLA(ctx, userName, la); err != nil { return nil, errors.Wrapf(err, "failed to update linked account") } } @@ -248,8 +248,8 @@ func (c *CommandHandler) RefreshLinkedAccount(ctx context.Context, rs *types.Rem // GetGitSource is a wrapper around common.GetGitSource that will also refresh // the oauth2 access token and update the linked account when needed -func (c *CommandHandler) GetGitSource(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (gitsource.GitSource, error) { - la, err := c.RefreshLinkedAccount(ctx, rs, userName, la) +func (h *ActionHandler) GetGitSource(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (gitsource.GitSource, error) { + la, err := h.RefreshLinkedAccount(ctx, rs, userName, la) if err != nil { return nil, err } @@ -265,7 +265,7 @@ type RegisterUserRequest struct { Oauth2AccessTokenExpiresAt time.Time } -func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequest) (*types.User, error) { +func (h *ActionHandler) RegisterUser(ctx context.Context, req *RegisterUserRequest) (*types.User, error) { if req.UserName == "" { return nil, util.NewErrBadRequest(errors.Errorf("user name required")) } @@ -273,11 +273,11 @@ func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequ return nil, util.NewErrBadRequest(errors.Errorf("invalid user name %q", req.UserName)) } - rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) + rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) } - c.log.Infof("rs: %s", util.Dump(rs)) + h.log.Infof("rs: %s", util.Dump(rs)) accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken) if err != nil { @@ -309,12 +309,12 @@ func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequ }, } - c.log.Infof("creating user account") - u, resp, err := c.configstoreClient.CreateUser(ctx, creq) + h.log.Infof("creating user account") + u, resp, err := h.configstoreClient.CreateUser(ctx, creq) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create linked account")) } - c.log.Infof("user %q created", req.UserName) + h.log.Infof("user %q created", req.UserName) return u, nil } @@ -332,12 +332,12 @@ type LoginUserResponse struct { User *types.User } -func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) { - rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) +func (h *ActionHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) { + rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) } - c.log.Infof("rs: %s", util.Dump(rs)) + h.log.Infof("rs: %s", util.Dump(rs)) accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken) if err != nil { @@ -356,7 +356,7 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) ( return nil, errors.Errorf("empty remote user id for remote source %q", rs.ID) } - user, resp, err := c.configstoreClient.GetUserByLinkedAccountRemoteUserAndSource(ctx, remoteUserInfo.ID, rs.ID) + user, resp, err := h.configstoreClient.GetUserByLinkedAccountRemoteUserAndSource(ctx, remoteUserInfo.ID, rs.ID) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user for remote user id %q and remote source %q", remoteUserInfo.ID, rs.ID)) } @@ -368,7 +368,7 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) ( break } } - c.log.Infof("la: %s", util.Dump(la)) + h.log.Infof("la: %s", util.Dump(la)) if la == nil { return nil, errors.Errorf("linked account for user %q for remote source %q doesn't exist", user.Name, rs.Name) } @@ -391,16 +391,16 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) ( Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt, } - c.log.Infof("updating user %q linked account", user.Name) - la, resp, err = c.configstoreClient.UpdateUserLA(ctx, user.Name, la.ID, creq) + h.log.Infof("updating user %q linked account", user.Name) + la, resp, err = h.configstoreClient.UpdateUserLA(ctx, user.Name, la.ID, creq) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to update user")) } - c.log.Infof("linked account %q for user %q updated", la.ID, user.Name) + h.log.Infof("linked account %q for user %q updated", la.ID, user.Name) } // generate jwt token - token, err := common.GenerateLoginJWTToken(c.sd, user.ID) + token, err := common.GenerateLoginJWTToken(h.sd, user.ID) if err != nil { return nil, err } @@ -423,12 +423,12 @@ type AuthorizeResponse struct { RemoteSourceName string } -func (c *CommandHandler) Authorize(ctx context.Context, req *AuthorizeRequest) (*AuthorizeResponse, error) { - rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) +func (h *ActionHandler) Authorize(ctx context.Context, req *AuthorizeRequest) (*AuthorizeResponse, error) { + rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) } - c.log.Infof("rs: %s", util.Dump(rs)) + h.log.Infof("rs: %s", util.Dump(rs)) accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken) if err != nil { @@ -458,17 +458,17 @@ type RemoteSourceAuthResponse struct { Response interface{} } -func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourceName, loginName, loginPassword string, requestType RemoteSourceRequestType, req interface{}) (*RemoteSourceAuthResponse, error) { - rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, remoteSourceName) +func (h *ActionHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourceName, loginName, loginPassword string, requestType RemoteSourceRequestType, req interface{}) (*RemoteSourceAuthResponse, error) { + rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceName) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName)) } - c.log.Infof("rs: %s", util.Dump(rs)) + h.log.Infof("rs: %s", util.Dump(rs)) switch requestType { case RemoteSourceRequestTypeCreateUserLA: req := req.(*CreateUserLARequest) - user, resp, err := c.configstoreClient.GetUser(ctx, req.UserRef) + user, resp, err := h.configstoreClient.GetUser(ctx, req.UserRef) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.UserRef)) } @@ -479,7 +479,7 @@ func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourc break } } - c.log.Infof("la: %s", util.Dump(la)) + h.log.Infof("la: %s", util.Dump(la)) if la != nil { return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", req.UserRef, rs.Name)) } @@ -500,15 +500,15 @@ func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourc if err != nil { return nil, errors.Wrapf(err, "failed to create git source") } - token, err := common.GenerateJWTToken(c.sd, rs.Name, string(requestType), req) + token, err := common.GenerateJWTToken(h.sd, rs.Name, string(requestType), req) if err != nil { return nil, err } - redirect, err := oauth2Source.GetOauth2AuthorizationURL(c.webExposedURL+"/oauth2/callback", token) + redirect, err := oauth2Source.GetOauth2AuthorizationURL(h.webExposedURL+"/oauth2/callback", token) if err != nil { return nil, err } - c.log.Infof("oauth2 redirect: %s", redirect) + h.log.Infof("oauth2 redirect: %s", redirect) return &RemoteSourceAuthResponse{ Oauth2Redirect: redirect, @@ -519,17 +519,17 @@ func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourc if err != nil { return nil, errors.Wrapf(err, "failed to create git source") } - tokenName := "agola-" + c.agolaID + tokenName := "agola-" + h.agolaID accessToken, err := passwordSource.LoginPassword(loginName, loginPassword, tokenName) if err != nil { return nil, errors.Wrapf(err, "failed to login to remote source %q with login name %q", rs.Name, loginName) } - c.log.Infof("access token: %s", accessToken) + h.log.Infof("access token: %s", accessToken) requestj, err := json.Marshal(req) if err != nil { return nil, err } - cres, err := c.HandleRemoteSourceAuthRequest(ctx, requestType, string(requestj), accessToken, "", "", time.Time{}) + cres, err := h.HandleRemoteSourceAuthRequest(ctx, requestType, string(requestj), accessToken, "", "", time.Time{}) if err != nil { return nil, err } @@ -560,7 +560,7 @@ type CreateUserLAResponse struct { LinkedAccount *types.LinkedAccount } -func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requestType RemoteSourceRequestType, requestString string, userAccessToken, oauth2AccessToken, oauth2RefreshToken string, oauth2AccessTokenExpiresAt time.Time) (*RemoteSourceAuthResult, error) { +func (h *ActionHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requestType RemoteSourceRequestType, requestString string, userAccessToken, oauth2AccessToken, oauth2RefreshToken string, oauth2AccessTokenExpiresAt time.Time) (*RemoteSourceAuthResult, error) { switch requestType { case RemoteSourceRequestTypeCreateUserLA: var req *CreateUserLARequest @@ -576,7 +576,7 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ Oauth2RefreshToken: oauth2RefreshToken, Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt, } - la, err := c.CreateUserLA(ctx, creq) + la, err := h.CreateUserLA(ctx, creq) if err != nil { return nil, err } @@ -601,7 +601,7 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ Oauth2RefreshToken: oauth2RefreshToken, Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt, } - cresp, err := c.RegisterUser(ctx, creq) + cresp, err := h.RegisterUser(ctx, creq) if err != nil { return nil, err } @@ -623,7 +623,7 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ Oauth2RefreshToken: oauth2RefreshToken, Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt, } - cresp, err := c.LoginUser(ctx, creq) + cresp, err := h.LoginUser(ctx, creq) if err != nil { return nil, err } @@ -645,7 +645,7 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ Oauth2RefreshToken: oauth2RefreshToken, Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt, } - cresp, err := c.Authorize(ctx, creq) + cresp, err := h.Authorize(ctx, creq) if err != nil { return nil, err } @@ -659,19 +659,19 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ } } -func (c *CommandHandler) HandleOauth2Callback(ctx context.Context, code, state string) (*RemoteSourceAuthResult, error) { +func (h *ActionHandler) HandleOauth2Callback(ctx context.Context, code, state string) (*RemoteSourceAuthResult, error) { token, err := jwt.Parse(state, func(token *jwt.Token) (interface{}, error) { - if token.Method != c.sd.Method { + if token.Method != h.sd.Method { return nil, errors.Errorf("unexpected signing method: %v", token.Header["alg"]) } var key interface{} - switch c.sd.Method { + switch h.sd.Method { case jwt.SigningMethodRS256: - key = c.sd.PrivateKey + key = h.sd.PrivateKey case jwt.SigningMethodHS256: - key = c.sd.Key + key = h.sd.Key default: - return nil, errors.Errorf("unsupported signing method %q", c.sd.Method.Alg()) + return nil, errors.Errorf("unsupported signing method %q", h.sd.Method.Alg()) } return key, nil }) @@ -687,21 +687,21 @@ func (c *CommandHandler) HandleOauth2Callback(ctx context.Context, code, state s requestType := RemoteSourceRequestType(claims["request_type"].(string)) requestString := claims["request"].(string) - rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, remoteSourceName) + rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceName) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName)) } - c.log.Infof("rs: %s", util.Dump(rs)) + h.log.Infof("rs: %s", util.Dump(rs)) oauth2Source, err := common.GetOauth2Source(rs, "") if err != nil { return nil, errors.Wrapf(err, "failed to create gitlab source") } - oauth2Token, err := oauth2Source.RequestOauth2Token(c.webExposedURL+"/oauth2/callback", code) + oauth2Token, err := oauth2Source.RequestOauth2Token(h.webExposedURL+"/oauth2/callback", code) if err != nil { return nil, err } - return c.HandleRemoteSourceAuthRequest(ctx, requestType, requestString, "", oauth2Token.AccessToken, oauth2Token.RefreshToken, oauth2Token.Expiry) + return h.HandleRemoteSourceAuthRequest(ctx, requestType, requestString, "", oauth2Token.AccessToken, oauth2Token.RefreshToken, oauth2Token.Expiry) } diff --git a/internal/services/gateway/api/oauth2.go b/internal/services/gateway/api/oauth2.go index 49fba7c..f4b2925 100644 --- a/internal/services/gateway/api/oauth2.go +++ b/internal/services/gateway/api/oauth2.go @@ -18,7 +18,7 @@ import ( "net/http" csapi "github.com/sorintlab/agola/internal/services/configstore/api" - "github.com/sorintlab/agola/internal/services/gateway/command" + "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/util" "go.uber.org/zap" @@ -26,7 +26,7 @@ import ( type OAuth2CallbackHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler configstoreClient *csapi.Client } @@ -35,8 +35,8 @@ type RemoteSourceAuthResult struct { Response interface{} `json:"response,omitempty"` } -func NewOAuth2CallbackHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client) *OAuth2CallbackHandler { - return &OAuth2CallbackHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient} +func NewOAuth2CallbackHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client) *OAuth2CallbackHandler { + return &OAuth2CallbackHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient} } func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -45,7 +45,7 @@ func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request code := query.Get("code") state := query.Get("state") - cresp, err := h.ch.HandleOauth2Callback(ctx, code, state) + cresp, err := h.ah.HandleOauth2Callback(ctx, code, state) if err != nil { h.log.Errorf("err: %+v", err) httpError(w, util.NewErrBadRequest(err)) @@ -54,27 +54,27 @@ func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request var response interface{} switch cresp.RequestType { - case command.RemoteSourceRequestTypeCreateUserLA: - authresp := cresp.Response.(*command.CreateUserLAResponse) + case action.RemoteSourceRequestTypeCreateUserLA: + authresp := cresp.Response.(*action.CreateUserLAResponse) response = &CreateUserLAResponse{ LinkedAccount: authresp.LinkedAccount, } - case command.RemoteSourceRequestTypeLoginUser: - authresp := cresp.Response.(*command.LoginUserResponse) + case action.RemoteSourceRequestTypeLoginUser: + authresp := cresp.Response.(*action.LoginUserResponse) response = &LoginUserResponse{ Token: authresp.Token, User: createUserResponse(authresp.User), } - case command.RemoteSourceRequestTypeAuthorize: - authresp := cresp.Response.(*command.AuthorizeResponse) + case action.RemoteSourceRequestTypeAuthorize: + authresp := cresp.Response.(*action.AuthorizeResponse) response = &AuthorizeResponse{ RemoteUserInfo: authresp.RemoteUserInfo, RemoteSourceName: authresp.RemoteSourceName, } - case command.RemoteSourceRequestTypeRegisterUser: + case action.RemoteSourceRequestTypeRegisterUser: response = &RegisterUserResponse{} } diff --git a/internal/services/gateway/api/org.go b/internal/services/gateway/api/org.go index bd56c2d..dd99c4d 100644 --- a/internal/services/gateway/api/org.go +++ b/internal/services/gateway/api/org.go @@ -21,7 +21,7 @@ import ( "github.com/pkg/errors" csapi "github.com/sorintlab/agola/internal/services/configstore/api" - "github.com/sorintlab/agola/internal/services/gateway/command" + "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/util" "go.uber.org/zap" @@ -35,11 +35,11 @@ type CreateOrgRequest struct { type CreateOrgHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler } -func NewCreateOrgHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateOrgHandler { - return &CreateOrgHandler{log: logger.Sugar(), ch: ch} +func NewCreateOrgHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateOrgHandler { + return &CreateOrgHandler{log: logger.Sugar(), ah: ah} } func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -59,12 +59,12 @@ func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - creq := &command.CreateOrgRequest{ + creq := &action.CreateOrgRequest{ Name: req.Name, CreatorUserID: userID, } - org, err := h.ch.CreateOrg(ctx, creq) + org, err := h.ah.CreateOrg(ctx, creq) if httpError(w, err) { h.log.Errorf("err: %+v", err) return diff --git a/internal/services/gateway/api/project.go b/internal/services/gateway/api/project.go index 992246a..7b00bc3 100644 --- a/internal/services/gateway/api/project.go +++ b/internal/services/gateway/api/project.go @@ -21,7 +21,7 @@ import ( "github.com/pkg/errors" csapi "github.com/sorintlab/agola/internal/services/configstore/api" - "github.com/sorintlab/agola/internal/services/gateway/command" + "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/util" @@ -40,13 +40,13 @@ type CreateProjectRequest struct { type CreateProjectHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler configstoreClient *csapi.Client exposedURL string } -func NewCreateProjectHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectHandler { - return &CreateProjectHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient, exposedURL: exposedURL} +func NewCreateProjectHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectHandler { + return &CreateProjectHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient, exposedURL: exposedURL} } func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -67,7 +67,7 @@ func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) userID := userIDVal.(string) h.log.Infof("userID: %q", userID) - creq := &command.CreateProjectRequest{ + areq := &action.CreateProjectRequest{ Name: req.Name, ParentID: req.ParentID, Visibility: req.Visibility, @@ -77,7 +77,7 @@ func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck, } - project, err := h.ch.CreateProject(ctx, creq) + project, err := h.ah.CreateProject(ctx, areq) if httpError(w, err) { h.log.Errorf("err: %+v", err) return @@ -91,13 +91,13 @@ func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) type ProjectReconfigHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler configstoreClient *csapi.Client exposedURL string } -func NewProjectReconfigHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client, exposedURL string) *ProjectReconfigHandler { - return &ProjectReconfigHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient, exposedURL: exposedURL} +func NewProjectReconfigHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client, exposedURL string) *ProjectReconfigHandler { + return &ProjectReconfigHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient, exposedURL: exposedURL} } func (h *ProjectReconfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -109,7 +109,7 @@ func (h *ProjectReconfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques return } - if err := h.ch.ReconfigProject(ctx, projectRef); err != nil { + if err := h.ah.ReconfigProject(ctx, projectRef); err != nil { httpError(w, err) return } diff --git a/internal/services/gateway/api/projectgroup.go b/internal/services/gateway/api/projectgroup.go index 1a63fea..699eec3 100644 --- a/internal/services/gateway/api/projectgroup.go +++ b/internal/services/gateway/api/projectgroup.go @@ -21,7 +21,7 @@ import ( "github.com/pkg/errors" csapi "github.com/sorintlab/agola/internal/services/configstore/api" - "github.com/sorintlab/agola/internal/services/gateway/command" + "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/util" @@ -37,13 +37,13 @@ type CreateProjectGroupRequest struct { type CreateProjectGroupHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler configstoreClient *csapi.Client exposedURL string } -func NewCreateProjectGroupHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectGroupHandler { - return &CreateProjectGroupHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient, exposedURL: exposedURL} +func NewCreateProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectGroupHandler { + return &CreateProjectGroupHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient, exposedURL: exposedURL} } func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -64,14 +64,14 @@ func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Req userID := userIDVal.(string) h.log.Infof("userID: %q", userID) - creq := &command.CreateProjectGroupRequest{ + creq := &action.CreateProjectGroupRequest{ Name: req.Name, ParentID: req.ParentID, Visibility: req.Visibility, CurrentUserID: userID, } - projectGroup, err := h.ch.CreateProjectGroup(ctx, creq) + projectGroup, err := h.ah.CreateProjectGroup(ctx, creq) if httpError(w, err) { h.log.Errorf("err: %+v", err) return diff --git a/internal/services/gateway/api/remoterepo.go b/internal/services/gateway/api/remoterepo.go index 19e1a98..86b0ab2 100644 --- a/internal/services/gateway/api/remoterepo.go +++ b/internal/services/gateway/api/remoterepo.go @@ -19,7 +19,7 @@ import ( gitsource "github.com/sorintlab/agola/internal/gitsources" csapi "github.com/sorintlab/agola/internal/services/configstore/api" - "github.com/sorintlab/agola/internal/services/gateway/command" + "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/util" @@ -44,12 +44,12 @@ func createRemoteRepoResponse(r *gitsource.RepoInfo) *RemoteRepoResponse { type UserRemoteReposHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler configstoreClient *csapi.Client } -func NewUserRemoteReposHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client) *UserRemoteReposHandler { - return &UserRemoteReposHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient} +func NewUserRemoteReposHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client) *UserRemoteReposHandler { + return &UserRemoteReposHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient} } func (h *UserRemoteReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -91,7 +91,7 @@ func (h *UserRemoteReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques return } - gitsource, err := h.ch.GetGitSource(ctx, rs, user.Name, la) + gitsource, err := h.ah.GetGitSource(ctx, rs, user.Name, la) if err != nil { httpError(w, util.NewErrBadRequest(errors.Wrapf(err, "failed to create gitsource client"))) return diff --git a/internal/services/gateway/api/remotesource.go b/internal/services/gateway/api/remotesource.go index 214dba4..2033a87 100644 --- a/internal/services/gateway/api/remotesource.go +++ b/internal/services/gateway/api/remotesource.go @@ -21,7 +21,7 @@ import ( "github.com/pkg/errors" csapi "github.com/sorintlab/agola/internal/services/configstore/api" - "github.com/sorintlab/agola/internal/services/gateway/command" + "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/util" "go.uber.org/zap" @@ -40,11 +40,11 @@ type CreateRemoteSourceRequest struct { type CreateRemoteSourceHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler } -func NewCreateRemoteSourceHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateRemoteSourceHandler { - return &CreateRemoteSourceHandler{log: logger.Sugar(), ch: ch} +func NewCreateRemoteSourceHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateRemoteSourceHandler { + return &CreateRemoteSourceHandler{log: logger.Sugar(), ah: ah} } func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -57,7 +57,7 @@ func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Req return } - creq := &command.CreateRemoteSourceRequest{ + creq := &action.CreateRemoteSourceRequest{ Name: req.Name, APIURL: req.APIURL, Type: req.Type, @@ -65,7 +65,7 @@ func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Req Oauth2ClientID: req.Oauth2ClientID, Oauth2ClientSecret: req.Oauth2ClientSecret, } - rs, err := h.ch.CreateRemoteSource(ctx, creq) + rs, err := h.ah.CreateRemoteSource(ctx, creq) if httpError(w, err) { h.log.Errorf("err: %+v", err) return diff --git a/internal/services/gateway/api/user.go b/internal/services/gateway/api/user.go index 6c1a2c3..61d3ece 100644 --- a/internal/services/gateway/api/user.go +++ b/internal/services/gateway/api/user.go @@ -24,7 +24,7 @@ import ( "github.com/pkg/errors" gitsource "github.com/sorintlab/agola/internal/gitsources" csapi "github.com/sorintlab/agola/internal/services/configstore/api" - "github.com/sorintlab/agola/internal/services/gateway/command" + "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/util" "go.uber.org/zap" @@ -38,11 +38,11 @@ type CreateUserRequest struct { type CreateUserHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler } -func NewCreateUserHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateUserHandler { - return &CreateUserHandler{log: logger.Sugar(), ch: ch} +func NewCreateUserHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserHandler { + return &CreateUserHandler{log: logger.Sugar(), ah: ah} } func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -55,11 +55,11 @@ func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - creq := &command.CreateUserRequest{ + creq := &action.CreateUserRequest{ UserName: req.UserName, } - u, err := h.ch.CreateUser(ctx, creq) + u, err := h.ah.CreateUser(ctx, creq) if httpError(w, err) { h.log.Errorf("err: %+v", err) return @@ -252,11 +252,11 @@ type CreateUserLAResponse struct { type CreateUserLAHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler } -func NewCreateUserLAHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateUserLAHandler { - return &CreateUserLAHandler{log: logger.Sugar(), ch: ch} +func NewCreateUserLAHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserLAHandler { + return &CreateUserLAHandler{log: logger.Sugar(), ah: ah} } func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -283,13 +283,13 @@ func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) } func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userRef string, req *CreateUserLARequest) (*CreateUserLAResponse, error) { - creq := &command.CreateUserLARequest{ + creq := &action.CreateUserLARequest{ UserRef: userRef, RemoteSourceName: req.RemoteSourceName, } h.log.Infof("creating linked account") - cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.RemoteSourceLoginName, req.RemoteSourceLoginPassword, command.RemoteSourceRequestTypeCreateUserLA, creq) + cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.RemoteSourceLoginName, req.RemoteSourceLoginPassword, action.RemoteSourceRequestTypeCreateUserLA, creq) if err != nil { return nil, err } @@ -298,7 +298,7 @@ func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userRef string, Oauth2Redirect: cresp.Oauth2Redirect, }, nil } - authresp := cresp.Response.(*command.CreateUserLAResponse) + authresp := cresp.Response.(*action.CreateUserLAResponse) resp := &CreateUserLAResponse{ LinkedAccount: authresp.LinkedAccount, @@ -343,11 +343,11 @@ type CreateUserTokenResponse struct { type CreateUserTokenHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler } -func NewCreateUserTokenHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateUserTokenHandler { - return &CreateUserTokenHandler{log: logger.Sugar(), ch: ch} +func NewCreateUserTokenHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserTokenHandler { + return &CreateUserTokenHandler{log: logger.Sugar(), ah: ah} } func (h *CreateUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -362,12 +362,12 @@ func (h *CreateUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques return } - creq := &command.CreateUserTokenRequest{ + creq := &action.CreateUserTokenRequest{ UserRef: userRef, TokenName: req.TokenName, } h.log.Infof("creating user %q token", userRef) - token, err := h.ch.CreateUserToken(ctx, creq) + token, err := h.ah.CreateUserToken(ctx, creq) if httpError(w, err) { h.log.Errorf("err: %+v", err) return @@ -416,15 +416,15 @@ type RegisterUserRequest struct { type RegisterUserHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler } type RegisterUserResponse struct { Oauth2Redirect string `json:"oauth2_redirect"` } -func NewRegisterUserHandler(logger *zap.Logger, ch *command.CommandHandler) *RegisterUserHandler { - return &RegisterUserHandler{log: logger.Sugar(), ch: ch} +func NewRegisterUserHandler(logger *zap.Logger, ah *action.ActionHandler) *RegisterUserHandler { + return &RegisterUserHandler{log: logger.Sugar(), ah: ah} } func (h *RegisterUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -449,12 +449,12 @@ func (h *RegisterUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) } func (h *RegisterUserHandler) registerUser(ctx context.Context, req *RegisterUserRequest) (*RegisterUserResponse, error) { - creq := &command.RegisterUserRequest{ + creq := &action.RegisterUserRequest{ UserName: req.CreateUserRequest.UserName, RemoteSourceName: req.CreateUserLARequest.RemoteSourceName, } - cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.CreateUserLARequest.RemoteSourceName, req.CreateUserLARequest.RemoteSourceLoginName, req.CreateUserLARequest.RemoteSourceLoginPassword, command.RemoteSourceRequestTypeRegisterUser, creq) + cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.CreateUserLARequest.RemoteSourceName, req.CreateUserLARequest.RemoteSourceLoginName, req.CreateUserLARequest.RemoteSourceLoginPassword, action.RemoteSourceRequestTypeRegisterUser, creq) if err != nil { return nil, err } @@ -463,7 +463,7 @@ func (h *RegisterUserHandler) registerUser(ctx context.Context, req *RegisterUse Oauth2Redirect: cresp.Oauth2Redirect, }, nil } - //authresp := cresp.Response.(*command.RegisterUserResponse) + //authresp := cresp.Response.(*action.RegisterUserResponse) resp := &RegisterUserResponse{} return resp, nil @@ -471,7 +471,7 @@ func (h *RegisterUserHandler) registerUser(ctx context.Context, req *RegisterUse type AuthorizeHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler } type AuthorizeResponse struct { @@ -480,8 +480,8 @@ type AuthorizeResponse struct { RemoteSourceName string `json:"remote_source_name"` } -func NewAuthorizeHandler(logger *zap.Logger, ch *command.CommandHandler) *AuthorizeHandler { - return &AuthorizeHandler{log: logger.Sugar(), ch: ch} +func NewAuthorizeHandler(logger *zap.Logger, ah *action.ActionHandler) *AuthorizeHandler { + return &AuthorizeHandler{log: logger.Sugar(), ah: ah} } func (h *AuthorizeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -506,11 +506,11 @@ func (h *AuthorizeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (h *AuthorizeHandler) authorize(ctx context.Context, req *LoginUserRequest) (*AuthorizeResponse, error) { - creq := &command.LoginUserRequest{ + creq := &action.LoginUserRequest{ RemoteSourceName: req.RemoteSourceName, } - cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, command.RemoteSourceRequestTypeAuthorize, creq) + cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, action.RemoteSourceRequestTypeAuthorize, creq) if err != nil { return nil, err } @@ -519,7 +519,7 @@ func (h *AuthorizeHandler) authorize(ctx context.Context, req *LoginUserRequest) Oauth2Redirect: cresp.Oauth2Redirect, }, nil } - authresp := cresp.Response.(*command.AuthorizeResponse) + authresp := cresp.Response.(*action.AuthorizeResponse) resp := &AuthorizeResponse{ RemoteUserInfo: authresp.RemoteUserInfo, @@ -542,11 +542,11 @@ type LoginUserResponse struct { type LoginUserHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler } -func NewLoginUserHandler(logger *zap.Logger, ch *command.CommandHandler) *LoginUserHandler { - return &LoginUserHandler{log: logger.Sugar(), ch: ch} +func NewLoginUserHandler(logger *zap.Logger, ah *action.ActionHandler) *LoginUserHandler { + return &LoginUserHandler{log: logger.Sugar(), ah: ah} } func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -571,12 +571,12 @@ func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) { - creq := &command.LoginUserRequest{ + creq := &action.LoginUserRequest{ RemoteSourceName: req.RemoteSourceName, } h.log.Infof("logging in user") - cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, command.RemoteSourceRequestTypeLoginUser, creq) + cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, action.RemoteSourceRequestTypeLoginUser, creq) if err != nil { return nil, err } @@ -585,7 +585,7 @@ func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest) Oauth2Redirect: cresp.Oauth2Redirect, }, nil } - authresp := cresp.Response.(*command.LoginUserResponse) + authresp := cresp.Response.(*action.LoginUserResponse) resp := &LoginUserResponse{ Token: authresp.Token, diff --git a/internal/services/gateway/gateway.go b/internal/services/gateway/gateway.go index de25359..859dc7b 100644 --- a/internal/services/gateway/gateway.go +++ b/internal/services/gateway/gateway.go @@ -25,8 +25,8 @@ import ( "github.com/sorintlab/agola/internal/objectstorage" "github.com/sorintlab/agola/internal/services/config" csapi "github.com/sorintlab/agola/internal/services/configstore/api" + "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/gateway/api" - "github.com/sorintlab/agola/internal/services/gateway/command" "github.com/sorintlab/agola/internal/services/gateway/common" "github.com/sorintlab/agola/internal/services/gateway/handlers" rsapi "github.com/sorintlab/agola/internal/services/runservice/scheduler/api" @@ -54,7 +54,7 @@ type Gateway struct { ost *objectstorage.ObjStorage runserviceClient *rsapi.Client configstoreClient *csapi.Client - ch *command.CommandHandler + ah *action.ActionHandler sd *common.TokenSigningData } @@ -123,14 +123,14 @@ func NewGateway(gc *config.Config) (*Gateway, error) { configstoreClient := csapi.NewClient(c.ConfigStoreURL) - ch := command.NewCommandHandler(logger, sd, configstoreClient, gc.ID, c.APIExposedURL, c.WebExposedURL) + ah := action.NewActionHandler(logger, sd, configstoreClient, gc.ID, c.APIExposedURL, c.WebExposedURL) return &Gateway{ c: c, ost: ost, runserviceClient: rsapi.NewClient(c.RunServiceURL), configstoreClient: configstoreClient, - ch: ch, + ah: ah, sd: sd, }, nil } @@ -146,17 +146,17 @@ func (g *Gateway) Run(ctx context.Context) error { corsAllowedOriginsOptions := ghandlers.AllowedOrigins([]string{"*"}) corsHandler = ghandlers.CORS(corsAllowedMethodsOptions, corsAllowedHeadersOptions, corsAllowedOriginsOptions) - webhooksHandler := &webhooksHandler{log: log, ch: g.ch, configstoreClient: g.configstoreClient, runserviceClient: g.runserviceClient, apiExposedURL: g.c.APIExposedURL} + webhooksHandler := &webhooksHandler{log: log, ah: g.ah, configstoreClient: g.configstoreClient, runserviceClient: g.runserviceClient, apiExposedURL: g.c.APIExposedURL} projectGroupHandler := api.NewProjectGroupHandler(logger, g.configstoreClient) projectGroupSubgroupsHandler := api.NewProjectGroupSubgroupsHandler(logger, g.configstoreClient) projectGroupProjectsHandler := api.NewProjectGroupProjectsHandler(logger, g.configstoreClient) - createProjectGroupHandler := api.NewCreateProjectGroupHandler(logger, g.ch, g.configstoreClient, g.c.APIExposedURL) + createProjectGroupHandler := api.NewCreateProjectGroupHandler(logger, g.ah, g.configstoreClient, g.c.APIExposedURL) projectHandler := api.NewProjectHandler(logger, g.configstoreClient) - createProjectHandler := api.NewCreateProjectHandler(logger, g.ch, g.configstoreClient, g.c.APIExposedURL) + createProjectHandler := api.NewCreateProjectHandler(logger, g.ah, g.configstoreClient, g.c.APIExposedURL) deleteProjectHandler := api.NewDeleteProjectHandler(logger, g.configstoreClient) - projectReconfigHandler := api.NewProjectReconfigHandler(logger, g.ch, g.configstoreClient, g.c.APIExposedURL) + projectReconfigHandler := api.NewProjectReconfigHandler(logger, g.ah, g.configstoreClient, g.c.APIExposedURL) secretHandler := api.NewSecretHandler(logger, g.configstoreClient) createSecretHandler := api.NewCreateSecretHandler(logger, g.configstoreClient) @@ -169,21 +169,21 @@ func (g *Gateway) Run(ctx context.Context) error { currentUserHandler := api.NewCurrentUserHandler(logger, g.configstoreClient) userHandler := api.NewUserHandler(logger, g.configstoreClient) usersHandler := api.NewUsersHandler(logger, g.configstoreClient) - createUserHandler := api.NewCreateUserHandler(logger, g.ch) + createUserHandler := api.NewCreateUserHandler(logger, g.ah) deleteUserHandler := api.NewDeleteUserHandler(logger, g.configstoreClient) - createUserLAHandler := api.NewCreateUserLAHandler(logger, g.ch) + createUserLAHandler := api.NewCreateUserLAHandler(logger, g.ah) deleteUserLAHandler := api.NewDeleteUserLAHandler(logger, g.configstoreClient) - createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.ch) + createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.ah) deleteUserTokenHandler := api.NewDeleteUserTokenHandler(logger, g.configstoreClient) remoteSourceHandler := api.NewRemoteSourceHandler(logger, g.configstoreClient) - createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ch) + createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ah) remoteSourcesHandler := api.NewRemoteSourcesHandler(logger, g.configstoreClient) orgHandler := api.NewOrgHandler(logger, g.configstoreClient) orgsHandler := api.NewOrgsHandler(logger, g.configstoreClient) - createOrgHandler := api.NewCreateOrgHandler(logger, g.ch) + createOrgHandler := api.NewCreateOrgHandler(logger, g.ah) deleteOrgHandler := api.NewDeleteOrgHandler(logger, g.configstoreClient) runHandler := api.NewRunHandler(logger, g.runserviceClient) @@ -195,12 +195,12 @@ func (g *Gateway) Run(ctx context.Context) error { logsHandler := api.NewLogsHandler(logger, g.runserviceClient) reposHandler := api.NewReposHandler(logger, g.c.GitServerURL) - userRemoteReposHandler := api.NewUserRemoteReposHandler(logger, g.ch, g.configstoreClient) + userRemoteReposHandler := api.NewUserRemoteReposHandler(logger, g.ah, g.configstoreClient) - loginUserHandler := api.NewLoginUserHandler(logger, g.ch) - authorizeHandler := api.NewAuthorizeHandler(logger, g.ch) - registerHandler := api.NewRegisterUserHandler(logger, g.ch) - oauth2callbackHandler := api.NewOAuth2CallbackHandler(logger, g.ch, g.configstoreClient) + loginUserHandler := api.NewLoginUserHandler(logger, g.ah) + authorizeHandler := api.NewAuthorizeHandler(logger, g.ah) + registerHandler := api.NewRegisterUserHandler(logger, g.ah) + oauth2callbackHandler := api.NewOAuth2CallbackHandler(logger, g.ah, g.configstoreClient) router := mux.NewRouter() diff --git a/internal/services/gateway/webhook.go b/internal/services/gateway/webhook.go index 110b2ba..4b08289 100644 --- a/internal/services/gateway/webhook.go +++ b/internal/services/gateway/webhook.go @@ -26,7 +26,7 @@ import ( "github.com/sorintlab/agola/internal/gitsources/agolagit" "github.com/sorintlab/agola/internal/runconfig" csapi "github.com/sorintlab/agola/internal/services/configstore/api" - "github.com/sorintlab/agola/internal/services/gateway/command" + "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/gateway/common" rsapi "github.com/sorintlab/agola/internal/services/runservice/scheduler/api" rstypes "github.com/sorintlab/agola/internal/services/runservice/types" @@ -108,7 +108,7 @@ func genGroup(baseGroupType GroupType, baseGroupID string, webhookData *types.We type webhooksHandler struct { log *zap.SugaredLogger - ch *command.CommandHandler + ah *action.ActionHandler configstoreClient *csapi.Client runserviceClient *rsapi.Client apiExposedURL string @@ -167,7 +167,7 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) { return http.StatusInternalServerError, "", errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID) } - gitSource, err = h.ch.GetGitSource(ctx, rs, user.Name, la) + gitSource, err = h.ah.GetGitSource(ctx, rs, user.Name, la) if err != nil { return http.StatusInternalServerError, "", errors.Wrapf(err, "failed to create gitea client") }