Skip to content

Commit

Permalink
Clean up axops/tool
Browse files Browse the repository at this point in the history
- Try to avoid the way we are using a pointer field to point the struct itself.
  • Loading branch information
Tianhe Zhang authored and sytianhe committed Aug 23, 2017
1 parent f4772c1 commit 180d134
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 132 deletions.
3 changes: 1 addition & 2 deletions saas/axops/src/applatix.io/axops/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions saas/axops/src/applatix.io/axops/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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)
}
Expand Down
54 changes: 19 additions & 35 deletions saas/axops/src/applatix.io/axops/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -305,127 +305,111 @@ 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{}
err = json.Unmarshal(configBytes, bitbucket)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
bitbucket.Real = bitbucket
t = bitbucket
case tool.TypeGitLab:
gitlab := &tool.GitLabConfig{}
err = json.Unmarshal(configBytes, gitlab)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
gitlab.Real = gitlab
t = gitlab
case tool.TypeCodeCommit:
codecommit := &tool.CodeCommitConfig{}
err = json.Unmarshal(configBytes, codecommit)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
codecommit.Real = codecommit
t = codecommit
case tool.TypeGIT:
git := &tool.GitConfig{}
err = json.Unmarshal(configBytes, git)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
git.Real = git
t = git
case tool.TypeSMTP:
smtp := &tool.SMTPConfig{}
err = json.Unmarshal(configBytes, smtp)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
smtp.Real = smtp
t = smtp
case tool.TypeSlack:
slack := &tool.SlackConfig{}
err = json.Unmarshal(configBytes, slack)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
slack.Real = slack
t = slack
case tool.TypeSplunk:
splunk := &tool.SplunkConfig{}
err = json.Unmarshal(configBytes, splunk)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
splunk.Real = splunk
t = splunk
case tool.TypeSAML:
saml := &tool.SAMLConfig{}
err = json.Unmarshal(configBytes, saml)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
saml.Real = saml
t = saml
case tool.TypeServer:
cert := &tool.ServerCertConfig{}
err = json.Unmarshal(configBytes, cert)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
cert.Real = cert
t = cert
case tool.TypeDockerHub:
dockerhub := &tool.DockerHubConfig{}
err = json.Unmarshal(configBytes, dockerhub)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
dockerhub.Real = dockerhub
t = dockerhub
case tool.TypePrivateRegistry:
private := &tool.PrivateRegistryConfig{}
err = json.Unmarshal(configBytes, private)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
private.Real = private
t = private
case tool.TypeRoute53:
domain := &tool.DomainConfig{}
err = json.Unmarshal(configBytes, domain)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
domain.Real = domain
t = domain
case tool.TypeNexus:
nexus := &tool.NexusConfig{}
err = json.Unmarshal(configBytes, nexus)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
nexus.Real = nexus
t = nexus
case tool.TypeSecureKey:
securekey := &tool.SecureKeyConfig{}
err = json.Unmarshal(configBytes, securekey)
if err != nil {
return nil, axerror.ERR_API_INVALID_REQ.NewWithMessagef("%v", err)
}
securekey.Real = securekey
t = securekey
case tool.TypeJira:
jira := &tool.JiraConfig{}
err = json.Unmarshal(configBytes, jira)
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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -697,7 +681,7 @@ func UpdateScmTools() {

if len(tools) != 0 {
for _, t := range tools {
UpdateScmTool(t.(tool.Tool).GetID())
UpdateScmTool(t.GetID())
}
}
}
Expand All @@ -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())
}
}
}
Expand All @@ -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())
}
}
}
Expand All @@ -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())
}
}
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions saas/axops/src/applatix.io/axops/tool/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 180d134

Please sign in to comment.