Skip to content

Commit

Permalink
Merge pull request #2504 from sirotsinskuy/bug/hzn-register-node-name…
Browse files Browse the repository at this point in the history
…-patch

Issue 2480 - Change 'hzn register -m <name>' to set the node name for existing nodes
  • Loading branch information
linggao authored May 13, 2021
2 parents 4b15131 + bbc3a8c commit 52fe189
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cli/exchange/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ func NodeUpdate(org string, credToUse string, node string, filePath string) {
}
}

type NodeExchangePatchName struct {
Name string `json:"name"`
}

type NodeExchangePatchToken struct {
Token string `json:"token"`
}
Expand Down
10 changes: 10 additions & 0 deletions cli/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func DoIt(org, pattern, nodeIdTok, userPw, inputFile string, nodeOrgFromFlag str
exchangePattern := ""
httpCode := cliutils.ExchangeGet("Exchange", exchUrlBase, "orgs/"+org+"/nodes/"+nodeId, cliutils.OrgAndCreds(org, nodeIdTok), nil, &devicesResp)

var existingNodeName string
if httpCode != 200 {
if userPw == "" {
cliutils.Fatal(cliutils.CLI_INPUT_ERROR, msgPrinter.Sprintf("node '%s/%s' does not exist in the Exchange with the specified token, and the -u flag was not specified to provide exchange user credentials to create/update it.", org, nodeId))
Expand All @@ -219,6 +220,7 @@ func DoIt(org, pattern, nodeIdTok, userPw, inputFile string, nodeOrgFromFlag str
if nodeType != n.GetNodeType() {
cliutils.Fatal(cliutils.CLI_INPUT_ERROR, msgPrinter.Sprintf("Node type mismatch. The node type '%v' does not match the node type '%v' of the Exchange node %v.", nodeType, n.GetNodeType(), nId))
}
existingNodeName = n.Name
break
}
}
Expand All @@ -232,10 +234,18 @@ func DoIt(org, pattern, nodeIdTok, userPw, inputFile string, nodeOrgFromFlag str
if nodeType != n.GetNodeType() {
cliutils.Fatal(cliutils.CLI_INPUT_ERROR, msgPrinter.Sprintf("Node type mismatch. The node type '%v' does not match the node type '%v' of the Exchange node %v.", nodeType, n.GetNodeType(), nId))
}
existingNodeName = n.Name
break
}
}

if existingNodeName != "" && existingNodeName != nodeName {
msgPrinter.Printf("Updating node name...")
msgPrinter.Println()
patchNodeReq := cliexchange.NodeExchangePatchName{Name: nodeName}
cliutils.ExchangePutPost("Exchange", http.MethodPatch, cliutils.GetExchangeUrl(), "orgs/"+org+"/nodes/"+nodeId, cliutils.OrgAndCreds(org, nodeIdTok), []int{200, 201}, patchNodeReq, nil)
}

checkPattern := false
// Use the exchange node pattern if any
if pattern == "" {
Expand Down

0 comments on commit 52fe189

Please sign in to comment.