From eee6dae92979ad93b614be7238aaa5f5d28d54a6 Mon Sep 17 00:00:00 2001 From: Tianhe Zhang Date: Wed, 23 Aug 2017 11:32:35 -0700 Subject: [PATCH] Clean up axops/tool - Try to avoid the way we are using a pointer field to point the struct itself. --- saas/axops/src/applatix.io/axops/secret.go | 3 +- .../src/applatix.io/axops/secret/secret.go | 5 +- saas/axops/src/applatix.io/axops/tool.go | 54 +++------ .../src/applatix.io/axops/tool/interface.go | 19 +-- .../src/applatix.io/axops/tool/schema.go | 28 +---- saas/axops/src/applatix.io/axops/tool/tool.go | 111 +++++++++--------- .../axops/src/applatix.io/axops/tool/utils.go | 3 +- saas/axops/src/applatix.io/axops/utils.go | 6 +- 8 files changed, 97 insertions(+), 132 deletions(-) diff --git a/saas/axops/src/applatix.io/axops/secret.go b/saas/axops/src/applatix.io/axops/secret.go index de032daa1691..367b1a267f3d 100644 --- a/saas/axops/src/applatix.io/axops/secret.go +++ b/saas/axops/src/applatix.io/axops/secret.go @@ -202,13 +202,12 @@ func SecretUpdateKeyHandler(c *gin.Context) { Type: tool.TypeSecureKey, } keyConfig = &tool.SecureKeyConfig{base, newKey, "default", secret.SECRET_KEY_VERSION} - keyConfig.Real = keyConfig } else { keyConfig = keys[0].(*tool.SecureKeyConfig) keyConfig.PrivateKey = newKey keyConfig.Version = secret.SECRET_KEY_VERSION } - _, axErr, _ = keyConfig.Update() + axErr, _ = tool.Update(keyConfig) if axErr != nil { c.JSON(axerror.REST_INTERNAL_ERR, axerror.ERR_API_INTERNAL_ERROR.NewWithMessagef("Failed to persist the updated RSA secure key: %v", axErr)) return diff --git a/saas/axops/src/applatix.io/axops/secret/secret.go b/saas/axops/src/applatix.io/axops/secret/secret.go index 5f8deb797da9..40389978ea26 100644 --- a/saas/axops/src/applatix.io/axops/secret/secret.go +++ b/saas/axops/src/applatix.io/axops/secret/secret.go @@ -83,8 +83,7 @@ func LoadRSAKey() *axerror.AXError { } keyConfig := &tool.SecureKeyConfig{base, RSAKey, "default", SECRET_KEY_VERSION} - keyConfig.Real = keyConfig - _, axErr, _ := keyConfig.Create() + axErr, _ := tool.Create(keyConfig) if axErr != nil { panic(fmt.Sprintf("Failed to persist the newly created RSA secure key: %v", axErr)) } @@ -97,7 +96,7 @@ func LoadRSAKey() *axerror.AXError { CreateRSAKey() keyconfig.PrivateKey = RSAKey keyconfig.Version = SECRET_KEY_VERSION - _, axErr, _ = keyconfig.Update() + axErr, _ = tool.Update(keyconfig) if axErr != nil { return axerror.ERR_API_INTERNAL_ERROR.NewWithMessagef("Failed to persist the updated RSA secure key: %v", axErr) } diff --git a/saas/axops/src/applatix.io/axops/tool.go b/saas/axops/src/applatix.io/axops/tool.go index 514319af72f2..3560726f92de 100644 --- a/saas/axops/src/applatix.io/axops/tool.go +++ b/saas/axops/src/applatix.io/axops/tool.go @@ -83,12 +83,12 @@ func CreateToolWithType(toolType string) gin.HandlerFunc { return } - created, axErr, code := t.(tool.Tool).Create() + axErr, code := tool.Create(t.(tool.Tool)) if axErr != nil { c.JSON(code, axErr) return } - created.Omit() + t.(tool.Tool).Omit() c.JSON(code, t) service.UpdateServiceETag() @@ -295,7 +295,7 @@ func CreateJira() gin.HandlerFunc { return CreateToolWithType(tool.TypeJira) } -func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.AXError) { +func unmarshalTool(toolType string, configBytes []byte) (tool.Tool, *axerror.AXError) { var t tool.Tool var err error switch toolType { @@ -305,7 +305,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - github.Real = github t = github case tool.TypeBitBucket: bitbucket := &tool.BitbucketConfig{} @@ -313,7 +312,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - bitbucket.Real = bitbucket t = bitbucket case tool.TypeGitLab: gitlab := &tool.GitLabConfig{} @@ -321,7 +319,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - gitlab.Real = gitlab t = gitlab case tool.TypeCodeCommit: codecommit := &tool.CodeCommitConfig{} @@ -329,7 +326,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - codecommit.Real = codecommit t = codecommit case tool.TypeGIT: git := &tool.GitConfig{} @@ -337,7 +333,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - git.Real = git t = git case tool.TypeSMTP: smtp := &tool.SMTPConfig{} @@ -345,7 +340,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - smtp.Real = smtp t = smtp case tool.TypeSlack: slack := &tool.SlackConfig{} @@ -353,7 +347,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - slack.Real = slack t = slack case tool.TypeSplunk: splunk := &tool.SplunkConfig{} @@ -361,7 +354,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - splunk.Real = splunk t = splunk case tool.TypeSAML: saml := &tool.SAMLConfig{} @@ -369,7 +361,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - saml.Real = saml t = saml case tool.TypeServer: cert := &tool.ServerCertConfig{} @@ -377,7 +368,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - cert.Real = cert t = cert case tool.TypeDockerHub: dockerhub := &tool.DockerHubConfig{} @@ -385,7 +375,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - dockerhub.Real = dockerhub t = dockerhub case tool.TypePrivateRegistry: private := &tool.PrivateRegistryConfig{} @@ -393,7 +382,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - private.Real = private t = private case tool.TypeRoute53: domain := &tool.DomainConfig{} @@ -401,7 +389,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - domain.Real = domain t = domain case tool.TypeNexus: nexus := &tool.NexusConfig{} @@ -409,7 +396,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - nexus.Real = nexus t = nexus case tool.TypeSecureKey: securekey := &tool.SecureKeyConfig{} @@ -417,7 +403,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - securekey.Real = securekey t = securekey case tool.TypeJira: jira := &tool.JiraConfig{} @@ -425,7 +410,6 @@ func unmarshalTool(toolType string, configBytes []byte) (interface{}, *axerror.A if err != nil { return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err) } - jira.Real = jira t = jira default: return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("The %v is not supported type.", toolType) @@ -581,13 +565,13 @@ func PutTool(toolType string) gin.HandlerFunc { return } - updated, axErr, code := newTool.(tool.Tool).Update() + axErr, code := tool.Update(newTool.(tool.Tool)) if axErr != nil { c.JSON(code, axErr) return } else { - updated.(tool.Tool).Omit() - c.JSON(code, updated) + newTool.(tool.Tool).Omit() + c.JSON(code, newTool) service.UpdateServiceETag() service.UpdateTemplateETag() @@ -630,7 +614,7 @@ func DeleteTool(toolType string) gin.HandlerFunc { } } - if axErr, code := t.(tool.Tool).Delete(); axErr != nil { + if axErr, code := tool.Delete(t.(tool.Tool)); axErr != nil { c.JSON(code, axErr) return } else { @@ -697,7 +681,7 @@ func UpdateScmTools() { if len(tools) != 0 { for _, t := range tools { - UpdateScmTool(t.(tool.Tool).GetID()) + UpdateScmTool(t.GetID()) } } } @@ -710,10 +694,10 @@ func UpdateScmTool(id string) { } if t != nil { - if _, axErr, _ := t.(tool.Tool).Update(); axErr != nil { - utils.ErrorLog.Printf("[SCM] Update tool %v %v %v to devops failed: %v\n", t.(tool.Tool).GetID(), t.(tool.Tool).GetType(), t.(tool.Tool).GetURL(), axErr) + if axErr, _ := tool.Update(t); axErr != nil { + utils.ErrorLog.Printf("[SCM] Update tool %v %v %v to devops failed: %v\n", t.GetID(), t.GetType(), t.GetURL(), axErr) } else { - utils.InfoLog.Printf("[SCM] Updated the SCM configurration: %v", t.(tool.Tool).GetURL()) + utils.InfoLog.Printf("[SCM] Updated the SCM configurration: %v", t.GetURL()) } } } @@ -726,10 +710,10 @@ func PushNotificationConfig() { if len(tools) != 0 { for _, t := range tools { - if axErr, _ = t.(tool.Tool).PushUpdate(); axErr != nil { - panic(fmt.Sprintf("Init: Push tool %v to axnotification failed: %v", t.(tool.Tool).GetID(), axErr)) + if axErr, _ = t.PushUpdate(); axErr != nil { + panic(fmt.Sprintf("Init: Push tool %v to axnotification failed: %v", t.GetID(), axErr)) } else { - utils.InfoLog.Printf("Pushed the notification configuration successfully: %v", t.(tool.Tool).GetURL()) + utils.InfoLog.Printf("Pushed the notification configuration successfully: %v", t.GetURL()) } } } @@ -743,10 +727,10 @@ func ApplyAuthenticationConfig() { if len(tools) != 0 { for _, t := range tools { - if axErr, _ = t.(tool.Tool).PushUpdate(); axErr != nil { - panic(fmt.Sprintf("Init: Load authentication configuration %v failed: %v", t.(tool.Tool).GetID(), axErr)) + if axErr, _ = t.PushUpdate(); axErr != nil { + panic(fmt.Sprintf("Init: Load authentication configuration %v failed: %v", t.GetID(), axErr)) } else { - utils.InfoLog.Printf("Loaded the authentication configurration successfully: %v", t.(tool.Tool).GetURL()) + utils.InfoLog.Printf("Loaded the authentication configurration successfully: %v", t.GetURL()) } } } @@ -782,7 +766,7 @@ func TestTool() gin.HandlerFunc { } if old != nil { - oldTool := old.(tool.Tool) + oldTool := old // Copy fields from old tool t["type"] = oldTool.GetType() @@ -816,7 +800,7 @@ func TestTool() gin.HandlerFunc { return } - axErr, code := toolObj.(tool.Tool).Test() + axErr, code := toolObj.Test() if axErr != nil { if code == 401 { // UI has some special logic for 401, work around in the backend for now diff --git a/saas/axops/src/applatix.io/axops/tool/interface.go b/saas/axops/src/applatix.io/axops/tool/interface.go index fa37f2c32b7c..242a690f91d3 100644 --- a/saas/axops/src/applatix.io/axops/tool/interface.go +++ b/saas/axops/src/applatix.io/axops/tool/interface.go @@ -8,24 +8,25 @@ type Tool interface { GetURL() string GetCategory() string GetType() string - GetConfig() (string, *axerror.AXError, int) GetPassword() string - - Create() (Tool, *axerror.AXError, int) - Update() (Tool, *axerror.AXError, int) - Delete() (*axerror.AXError, int) + // Generat UUID, used by the Create method in ToolBase + GenerateUUID() + //Create(Tool) (*axerror.AXError, int) + //Update(Tool) (*axerror.AXError, int) + //Delete() (*axerror.AXError, int) // omit the sensitive information, eg. config secret key, password Omit() // test the connection or correctness, eg. test to see if the github credential is valid Test() (*axerror.AXError, int) - // persist the config - save() (*axerror.AXError, int) - // validate attributes, eg. type, category are required - validate() (*axerror.AXError, int) + //// persist the config + //save() (*axerror.AXError, int) + // pre-process the configuration, eg. fetch the repository list for SCM configs pre() (*axerror.AXError, int) + // validate attributes, eg. type, category are required + validate() (*axerror.AXError, int) // push the updated configuration to the 3rd party, eg. axnotification needs the SMTP config PushUpdate() (*axerror.AXError, int) // notify the configuration deletion to the 3rd part diff --git a/saas/axops/src/applatix.io/axops/tool/schema.go b/saas/axops/src/applatix.io/axops/tool/schema.go index 8dd1910cfbb9..a598d74c063c 100644 --- a/saas/axops/src/applatix.io/axops/tool/schema.go +++ b/saas/axops/src/applatix.io/axops/tool/schema.go @@ -38,7 +38,7 @@ type ToolDB struct { Config string `json:"config,omitempty"` } -func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { +func (t *ToolDB) ToTool() (Tool, *axerror.AXError) { config := t.Config switch t.Type { case TypeGitHub: @@ -47,7 +47,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeBitBucket: tool := &BitbucketConfig{} @@ -55,7 +54,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeGitLab: tool := &GitLabConfig{} @@ -63,7 +61,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeCodeCommit: tool := &CodeCommitConfig{} @@ -71,7 +68,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeGIT: tool := &GitConfig{} @@ -79,7 +75,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeSMTP: tool := &SMTPConfig{} @@ -87,7 +82,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeSlack: tool := &SlackConfig{} @@ -95,7 +89,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeSplunk: tool := &SplunkConfig{} @@ -103,7 +96,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeSAML: tool := &SAMLConfig{} @@ -111,7 +103,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeServer: tool := &ServerCertConfig{} @@ -119,7 +110,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeDockerHub: tool := &DockerHubConfig{} @@ -127,7 +117,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypePrivateRegistry: tool := &PrivateRegistryConfig{} @@ -135,7 +124,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeRoute53: tool := &DomainConfig{} @@ -143,7 +131,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeNexus: tool := &NexusConfig{} @@ -151,7 +138,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeSecureKey: tool := &SecureKeyConfig{} @@ -159,7 +145,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil case TypeJira: tool := &JiraConfig{} @@ -167,7 +152,6 @@ func (t *ToolDB) ToTool() (interface{}, *axerror.AXError) { if err != nil { return nil, axerror.ERR_API_INTERNAL_ERROR.NewWithMessage("Can not unmarshal the configuration.") } - tool.Real = tool return tool, nil default: return nil, axerror.ERR_AX_INTERNAL.NewWithMessagef("The %v is not supported type.", t.Type) @@ -189,7 +173,7 @@ func (t *ToolDB) delete() *axerror.AXError { return nil } -func GetToolByID(id string) (interface{}, *axerror.AXError) { +func GetToolByID(id string) (Tool, *axerror.AXError) { schemes, axErr := GetTools(map[string]interface{}{ ToolID: id, }) @@ -205,19 +189,19 @@ func GetToolByID(id string) (interface{}, *axerror.AXError) { return schemes[0], nil } -func GetToolsByType(toolType string) ([]interface{}, *axerror.AXError) { +func GetToolsByType(toolType string) ([]Tool, *axerror.AXError) { return GetTools(map[string]interface{}{ ToolType: toolType, }) } -func GetToolsByCategory(category string) ([]interface{}, *axerror.AXError) { +func GetToolsByCategory(category string) ([]Tool, *axerror.AXError) { return GetTools(map[string]interface{}{ ToolCategory: category, }) } -func GetTools(params map[string]interface{}) ([]interface{}, *axerror.AXError) { +func GetTools(params map[string]interface{}) ([]Tool, *axerror.AXError) { toolDBs := []ToolDB{} axErr := utils.Dbcl.Get(axdb.AXDBAppAXOPS, ToolTableName, params, &toolDBs) @@ -225,7 +209,7 @@ func GetTools(params map[string]interface{}) ([]interface{}, *axerror.AXError) { return nil, axErr } - tools := []interface{}{} + tools := []Tool{} for i, _ := range toolDBs { tool, axErr := toolDBs[i].ToTool() if axErr != nil { diff --git a/saas/axops/src/applatix.io/axops/tool/tool.go b/saas/axops/src/applatix.io/axops/tool/tool.go index ab19503635b1..081ef7cae6cf 100644 --- a/saas/axops/src/applatix.io/axops/tool/tool.go +++ b/saas/axops/src/applatix.io/axops/tool/tool.go @@ -24,7 +24,6 @@ type ToolBase struct { Category string `json:"category,omitempty"` Type string `json:"type,omitempty"` Password string `json:"password,omitempty"` - Real Tool `json:"-"` } func (t *ToolBase) GetID() string { @@ -43,92 +42,87 @@ func (t *ToolBase) GetType() string { return t.Type } -func (t *ToolBase) GetConfig() (string, *axerror.AXError, int) { - configStr, err := json.Marshal(t.Real) - if err != nil { - return "", axerror.ERR_AX_INTERNAL.NewWithMessage(fmt.Sprintf("Failed to marshal the tool object: %v", err)), axerror.REST_INTERNAL_ERR - } - return string(configStr), nil, axerror.REST_STATUS_OK -} - func (t *ToolBase) GetPassword() string { return t.Password } -func (t *ToolBase) Create() (Tool, *axerror.AXError, int) { - +func (t *ToolBase) GenerateUUID() { t.ID = utils.GenerateUUIDv1() +} - if err, code := t.Real.pre(); err != nil { - return nil, err, code +func Create(t Tool) (*axerror.AXError, int) { + t.GenerateUUID() + + if err, code := t.pre(); err != nil { + return err, code } - if err, code := t.validate(); err != nil { - return nil, err, code + if err, code := validate(t); err != nil { + return err, code } - if err, code := t.Real.validate(); err != nil { - return nil, err, code + if err, code := t.validate(); err != nil { + return err, code } - if err, code := t.Real.PushUpdate(); err != nil { - return nil, err, code + if err, code := t.PushUpdate(); err != nil { + return err, code } - if err, code := t.save(); err != nil { - return nil, err, code + if err, code := save(t); err != nil { + return err, code } - return t.Real, nil, axerror.REST_CREATE_OK + return nil, axerror.REST_CREATE_OK } -func (t *ToolBase) Update() (Tool, *axerror.AXError, int) { +func Update(t Tool) (*axerror.AXError, int) { - if err, code := t.Real.pre(); err != nil { - return nil, err, code + if err, code := t.pre(); err != nil { + return err, code } - if err, code := t.validate(); err != nil { - return nil, err, code + if err, code := validate(t); err != nil { + return err, code } - if err, code := t.Real.validate(); err != nil { - return nil, err, code + if err, code := t.validate(); err != nil { + return err, code } - if err, code := t.Real.PushUpdate(); err != nil { - return nil, err, code + if err, code := t.PushUpdate(); err != nil { + return err, code } - old, err := GetToolByID(t.ID) + old, err := GetToolByID(t.GetID()) if err != nil { - return nil, err, axerror.REST_INTERNAL_ERR + return err, axerror.REST_INTERNAL_ERR } if old == nil { - return nil, axerror.ERR_API_RESOURCE_NOT_FOUND.New(), axerror.REST_NOT_FOUND + return axerror.ERR_API_RESOURCE_NOT_FOUND.New(), axerror.REST_NOT_FOUND } oldTool := old.(Tool) - if err, code := t.Real.Post(oldTool, t.Real); err != nil { - return nil, err, code + if err, code := t.Post(oldTool, t); err != nil { + return err, code } - if err, code := t.save(); err != nil { - return nil, err, code + if err, code := save(t); err != nil { + return err, code } - return t.Real, nil, axerror.REST_STATUS_OK + return nil, axerror.REST_STATUS_OK } -func (t *ToolBase) Delete() (*axerror.AXError, int) { +func Delete(t Tool) (*axerror.AXError, int) { - if err, code := t.Real.pushDelete(); err != nil { + if err, code := t.pushDelete(); err != nil { return err, code } - old, err := GetToolByID(t.ID) + old, err := GetToolByID(t.GetID()) if err != nil { return err, axerror.REST_INTERNAL_ERR } @@ -139,18 +133,22 @@ func (t *ToolBase) Delete() (*axerror.AXError, int) { oldTool := old.(Tool) - if err, code := t.Real.Post(oldTool, nil); err != nil { + if err, code := t.Post(oldTool, nil); err != nil { return err, code } - if err, code := t.delete(); err != nil { + if err, code := toolDelete(t); err != nil { return err, code } return nil, axerror.REST_STATUS_OK } -func (t *ToolBase) delete() (*axerror.AXError, int) { +func (t *ToolBase) Post(old, new interface{}) (*axerror.AXError, int) { + return nil, axerror.REST_STATUS_OK +} + +func toolDelete(t Tool) (*axerror.AXError, int) { tool := &ToolDB{ ID: t.GetID(), @@ -163,8 +161,7 @@ func (t *ToolBase) delete() (*axerror.AXError, int) { return nil, axerror.REST_STATUS_OK } -func (t *ToolBase) save() (*axerror.AXError, int) { - +func save(t Tool) (*axerror.AXError, int) { tool := &ToolDB{ ID: t.GetID(), URL: t.GetURL(), @@ -172,7 +169,7 @@ func (t *ToolBase) save() (*axerror.AXError, int) { Type: t.GetType(), } - config, err, code := t.GetConfig() + config, err, code := getConfig(t) if err != nil { return err, code } @@ -186,27 +183,31 @@ func (t *ToolBase) save() (*axerror.AXError, int) { return nil, axerror.REST_STATUS_OK } -func (t *ToolBase) validate() (*axerror.AXError, int) { +func validate(t Tool) (*axerror.AXError, int) { - if t.Category == "" { + if t.GetCategory() == "" { return ErrToolMissingCategory, axerror.REST_BAD_REQ } - if t.Type == "" { + if t.GetType() == "" { return ErrToolMissingType, axerror.REST_BAD_REQ } - if t.URL == "" { + if t.GetURL() == "" { return ErrToolMissingUrl, axerror.REST_BAD_REQ } - if t.ID == "" { + if t.GetID() == "" { return ErrToolMissingID, axerror.REST_BAD_REQ } return nil, axerror.REST_STATUS_OK } -func (t *ToolBase) Post(old, new interface{}) (*axerror.AXError, int) { - return nil, axerror.REST_STATUS_OK +func getConfig(t Tool) (string, *axerror.AXError, int) { + configStr, err := json.Marshal(t) + if err != nil { + return "", axerror.ERR_AX_INTERNAL.NewWithMessage(fmt.Sprintf("Failed to marshal the tool object: %v", err)), axerror.REST_INTERNAL_ERR + } + return string(configStr), nil, axerror.REST_STATUS_OK } diff --git a/saas/axops/src/applatix.io/axops/tool/utils.go b/saas/axops/src/applatix.io/axops/tool/utils.go index 4a9d3b3268c2..ef94fbe5d44c 100644 --- a/saas/axops/src/applatix.io/axops/tool/utils.go +++ b/saas/axops/src/applatix.io/axops/tool/utils.go @@ -125,8 +125,7 @@ func AddExampleRepository() { gitHub := &GitHubConfig{} gitHub.ToolBase = toolBase example1 := &GitConfig{gitHub} - example1.Real = example1 - _, axErr, _ := example1.Create() + axErr, _ := Create(example1) if axErr != nil { utils.ErrorLog.Printf("Failed to load the example repository(%v).\n", repo) } else { diff --git a/saas/axops/src/applatix.io/axops/utils.go b/saas/axops/src/applatix.io/axops/utils.go index c35aba634abc..ea9d67fd044b 100644 --- a/saas/axops/src/applatix.io/axops/utils.go +++ b/saas/axops/src/applatix.io/axops/utils.go @@ -283,9 +283,8 @@ func LoadCustomCert() { Type: tool.TypeServer, } crtConfig := &tool.ServerCertConfig{base, crt, key} - crtConfig.Real = crtConfig - _, axErr, _ := crtConfig.Create() + axErr, _ := tool.Create(crtConfig) if axErr != nil { panic(fmt.Sprintf("Failed to persisted the newly created self-signed certificate: %v", axErr)) } @@ -546,8 +545,7 @@ func CreateDomainManagementTool() *axerror.AXError { domain := &tool.DomainConfig{&tool.ToolBase{}, nil, nil} domain.Category = tool.CategoryDomainManagement domain.Type = tool.TypeRoute53 - domain.Real = domain - _, axErr, _ := domain.Create() + axErr, _ := tool.Create(domain) if axErr != nil { return axErr }