Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions pkg/debugcmd/deployment_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package debugcmd

import (
"context"
"encoding/json"
"fmt"

"github.com/threefoldtech/zosbase/pkg/gridtypes"
)

type DeploymentGetRequest struct {
TwinID uint32 `json:"twin_id"`
ContractID uint64 `json:"contract_id"`
WithHistory bool `json:"withhistory"`
}

type WorkloadTransaction struct {
Seq int `json:"seq"`
Type string `json:"type"`
Name string `json:"name"`
Created gridtypes.Timestamp `json:"created"`
State gridtypes.ResultState `json:"state"`
Message string `json:"message"`
}

type DeploymentGetResponse struct {
Deployment gridtypes.Deployment `json:"deployment"`
History []WorkloadTransaction `json:"history,omitempty"`
}

func ParseDeploymentGetRequest(payload []byte) (DeploymentGetRequest, error) {
var req DeploymentGetRequest
if err := json.Unmarshal(payload, &req); err != nil {
return req, err
}
return req, nil
}

func DeploymentGet(ctx context.Context, deps Deps, req DeploymentGetRequest) (DeploymentGetResponse, error) {
if req.TwinID == 0 {
return DeploymentGetResponse{}, fmt.Errorf("twin_id is required")
}
if req.ContractID == 0 {
return DeploymentGetResponse{}, fmt.Errorf("contract_id is required")
}

deployment, err := deps.Provision.Get(ctx, req.TwinID, req.ContractID)
if err != nil {
return DeploymentGetResponse{}, err
}
if !req.WithHistory {
return DeploymentGetResponse{Deployment: deployment}, nil
}

history, err := deps.Provision.Changes(ctx, req.TwinID, req.ContractID)
if err != nil {
return DeploymentGetResponse{}, err
}

transactions := make([]WorkloadTransaction, 0, len(history))
for idx, wl := range history {
transactions = append(transactions, WorkloadTransaction{
Seq: idx + 1,
Type: string(wl.Type),
Name: string(wl.Name),
Created: wl.Result.Created,
State: wl.Result.State,
Message: wl.Result.Error,
})
}

return DeploymentGetResponse{Deployment: deployment, History: transactions}, nil
}
72 changes: 72 additions & 0 deletions pkg/debugcmd/deployments_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package debugcmd

import (
"context"
"encoding/json"
)

type DeploymentsListRequest struct {
TwinID uint32 `json:"twin_id"`
}

type DeploymentsListWorkload struct {
Type string `json:"type"`
Name string `json:"name"`
State string `json:"state"`
}

type DeploymentsListItem struct {
TwinID uint32 `json:"twin_id"`
ContractID uint64 `json:"contract_id"`
Workloads []DeploymentsListWorkload `json:"workloads"`
}

type DeploymentsListResponse struct {
Items []DeploymentsListItem `json:"items"`
}

func ParseDeploymentsListRequest(payload []byte) (DeploymentsListRequest, error) {
var req DeploymentsListRequest
if len(payload) == 0 {
return req, nil
}
// optional payload
_ = json.Unmarshal(payload, &req)
return req, nil
}

func DeploymentsList(ctx context.Context, deps Deps, req DeploymentsListRequest) (DeploymentsListResponse, error) {
twins := []uint32{req.TwinID}
if req.TwinID == 0 {
var err error
twins, err = deps.Provision.ListTwins(ctx)
if err != nil {
return DeploymentsListResponse{}, err
}
}

items := make([]DeploymentsListItem, 0)
for _, twin := range twins {
deployments, err := deps.Provision.List(ctx, twin)
if err != nil {
return DeploymentsListResponse{}, err
}
for _, d := range deployments {
workloads := make([]DeploymentsListWorkload, 0, len(d.Workloads))
for _, wl := range d.Workloads {
workloads = append(workloads, DeploymentsListWorkload{
Type: string(wl.Type),
Name: string(wl.Name),
State: string(wl.Result.State),
})
}
items = append(items, DeploymentsListItem{
TwinID: d.TwinID,
ContractID: d.ContractID,
Workloads: workloads,
})
}
}

return DeploymentsListResponse{Items: items}, nil
}
36 changes: 36 additions & 0 deletions pkg/debugcmd/deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package debugcmd

import (
"context"

"github.com/threefoldtech/zosbase/pkg"
"github.com/threefoldtech/zosbase/pkg/gridtypes"
"github.com/threefoldtech/zosbase/pkg/gridtypes/zos"
)

// Provision is the subset of the provision zbus interface used by debug commands.
type Provision interface {
ListTwins(ctx context.Context) ([]uint32, error)
List(ctx context.Context, twin uint32) ([]gridtypes.Deployment, error)
Get(ctx context.Context, twin uint32, contract uint64) (gridtypes.Deployment, error)
Changes(ctx context.Context, twin uint32, contract uint64) ([]gridtypes.Workload, error)
}

// VM is the subset of the vmd zbus interface used by debug commands.
type VM interface {
Exists(ctx context.Context, id string) bool
Inspect(ctx context.Context, id string) (pkg.VMInfo, error)
Logs(ctx context.Context, id string) (string, error)
LogsFull(ctx context.Context, id string) (string, error)
}

// Network is the subset of the network zbus interface used by debug commands.
type Network interface {
Namespace(ctx context.Context, id zos.NetID) string
}

type Deps struct {
Provision Provision
VM VM
Network Network
}
Loading
Loading