Skip to content

Commit

Permalink
feat: add deployment create
Browse files Browse the repository at this point in the history
Added a new command to create deployments.

Signed-off-by: Lars Rustand <[email protected]>
  • Loading branch information
rustand2 committed Oct 2, 2023
1 parent 9d23648 commit cc2949c
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
48 changes: 48 additions & 0 deletions client/deployments/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package deployments

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -81,6 +82,7 @@ const (
transferCompleteURL = "/api/management/v1/deployments/artifacts/directupload/:id/complete"
artifactURL = "/api/management/v1/deployments/artifacts/:id"
artifactDownloadURL = "/api/management/v1/deployments/artifacts/:id/download"
deploymentCreateURL = "/api/management/v1/deployments/deployments"
)

type Client struct {
Expand All @@ -91,6 +93,7 @@ type Client struct {
artifactsListURL string
artifactDeleteURL string
directUploadURL string
deploymentCreateURL string
client *http.Client
}

Expand All @@ -114,6 +117,7 @@ func NewClient(url string, skipVerify bool) *Client {
artifactsListURL: client.JoinURL(url, artifactsListURL),
artifactDeleteURL: client.JoinURL(url, artifactsDeleteURL),
directUploadURL: client.JoinURL(url, directUploadURL),
deploymentCreateURL: client.JoinURL(url, deploymentCreateURL),
client: client.NewHttpClient(skipVerify),
}
}
Expand Down Expand Up @@ -358,6 +362,50 @@ func (c *Client) UploadArtifact(
return nil
}

func (c *Client) CreateDeployment(
deploymentName, artifactID, deviceList, token string,
) error {

deploymentJson := "{\"name\": \"" + deploymentName + "\", \"artifact_name\": \"" + artifactID + "\", \"devices\": " + deviceList + "}"
//"{\"token2fa\":\"" + token + "\"}"
reader := bytes.NewReader([]byte(deploymentJson))

req, err := http.NewRequest(http.MethodPost, c.deploymentCreateURL, reader)
if err != nil {
return errors.Wrap(err, "Cannot create request")
}
req.Header.Set("Authorization", "Bearer "+string(token))
req.Header.Set("Content-Type", "application/json")

reqDump, _ := httputil.DumpRequest(req, false)
log.Verbf("sending request: \n%v", string(reqDump))

rsp, err := c.client.Do(req)
if err != nil {
return errors.Wrap(err, "POST /deployments request failed")
}
defer rsp.Body.Close()

rspDump, _ := httputil.DumpResponse(rsp, true)
log.Verbf("response: \n%v\n", string(rspDump))

if rsp.StatusCode != http.StatusNoContent {
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return errors.Wrap(err, "can't read request body")
}
if rsp.StatusCode == http.StatusUnauthorized {
log.Verbf("deployment create failed with status %d, reason: %s", rsp.StatusCode, body)
return errors.New("Unauthorized. Please Login first")
}
return errors.New(
fmt.Sprintf("deployment create failed with status %d, reason: %s", rsp.StatusCode, body),
)
}

return nil
}

func (c *Client) DeleteArtifact(
artifactID, token string,
) error {
Expand Down
98 changes: 98 additions & 0 deletions cmd/deployment_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2023 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd

import (
"github.com/pkg/errors"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/mendersoftware/mender-cli/client/deployments"
"github.com/mendersoftware/mender-cli/log"
)

const (
argDeploymentName = "deployment-name"
)

var deploymentCreateCmd = &cobra.Command{
Use: "create [flags] DEPLOYMENT_NAME ARTIFACT_NAME DEVICE_LIST",
Short: "Create a new deployment from an existing artifact on the Mender server.",
Run: func(c *cobra.Command, args []string) {
cmd, err := CreateDeploymentCmd(c, args)
CheckErr(err)
CheckErr(cmd.Run())
},
}

func init() {
deploymentCreateCmd.Flags().StringP(argDeploymentName, "", "",
"destination path to download to")
}

type DeploymentCreateCmd struct {
server string
deploymentName string
artifactID string
deviceList string
token string
}

func CreateDeploymentCmd(cmd *cobra.Command, args []string) (*DeploymentCreateCmd, error) {
server := viper.GetString(argRootServer)
if server == "" {
return nil, errors.New("No server")
}

deploymentName = ""
if len(args) > 1 {
deploymentName = args[0]
}

artifactID := ""
if len(args) > 2 {
artifactID = args[1]
}

deviceList := ""
if len(args) >= 3 {
deviceList = args[2]
}

token, err := getAuthToken(cmd)
if err != nil {
return nil, err
}

return &DeploymentCreateCmd{
server: server,
deploymentName: deploymentName,
artifactID: artifactID,
deviceList: deviceList,
token: token,
}, nil
}

func (c *DeploymentCreateCmd) Run() error {
client := deployments.NewClient(c.server, false)
err := client.CreateDeployment(c.deploymentName, c.artifactID, c.deviceList, c.token)
if err != nil {
return err
}

log.Info("deployment created succesfully")

return nil
}
28 changes: 28 additions & 0 deletions cmd/deployments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2023 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd

import (
"github.com/spf13/cobra"
)

var deploymentsCmd = &cobra.Command{
Use: "deployments",
Short: "Operations on mender deployments.",
ValidArgs: []string{"create"},
}

func init() {
deploymentsCmd.AddCommand(deploymentCreateCmd)
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func init() {
rootCmd.AddCommand(loginCmd)
rootCmd.AddCommand(artifactsCmd)
rootCmd.AddCommand(devicesCmd)
rootCmd.AddCommand(deploymentsCmd)
rootCmd.AddCommand(terminalCmd)
rootCmd.AddCommand(portForwardCmd)
rootCmd.AddCommand(fileTransferCmd)
Expand Down

0 comments on commit cc2949c

Please sign in to comment.