Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

threefoldtecharchive/grid3_client_go

Repository files navigation

Grid3_client_go

Codacy Badge gopherbadger-tag-do-not-edit Testing Testing Dependabot

Grid3_client_go is a go client created to interact with threefold grid. It should manage CRUD operations for deployments on the grid.

Requirements

Go >= 1.19

Examples

This is a simple example to deploy a VM with a network.

import (
    "github.com/threefoldtech/grid3-go/deployer"
    "github.com/threefoldtech/grid3-go/workloads"
)

// Create Threefold plugin client
tfPluginClient, err := deployer.NewTFPluginClient(mnemonics, "sr25519", network, "", "", true, true)

// Get a free node to deploy
nodeID := 14

// Create a new network to deploy
network := workloads.ZNet{
    Name:        "newNetwork",
    Description: "A network to deploy",
    Nodes:       []uint32{nodeID},
    IPRange: gridtypes.NewIPNet(net.IPNet{
      IP:   net.IPv4(10, 1, 0, 0),
      Mask: net.CIDRMask(16, 32),
    }),
    AddWGAccess: true,
}

// Create a new VM to deploy
vm := workloads.VM{
    Name:       "vm",
    Flist:      "https://hub.grid.tf/tf-official-apps/base:latest.flist",
    CPU:        2,
    PublicIP:   true,
    Planetary:  true,
    Memory:     1024,
    RootfsSize: 20 * 1024,
    Entrypoint: "/sbin/zinit init",
    EnvVars: map[string]string{
        "SSH_KEY": publicKey,
    },
    IP:          "10.20.2.5",
    NetworkName: network.Name,
}

// Deploy the network first
err = tfPluginClient.NetworkDeployer.Deploy(context.Background(), &network)

// Load the network using the state loader
// this loader should load the deployment as json then convert it to a deployment go object with workloads inside it
networkObj, err := tfPluginClient.State.LoadNetworkFromGrid(network.Name)

// Deploy the VM deployment
dl := workloads.NewDeployment("vm", nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(ctx, &dl)

// Load the vm using the state loader
vmObj, err := tfPluginClient.State.LoadVMFromGrid(nodeID, vm.Name, dl.Name)

// Cancel the VM deployment
err = tfPluginClient.DeploymentDeployer.Cancel(ctx, &dl)

// Cancel the network
err = tfPluginClient.NetworkDeployer.Cancel(ctx, &network)

Refer to integration examples directory for more examples.

Run tests

To run the tests, export MNEMONICS and NETWORK

export MNEMONICS="<mnemonics words>"
export NETWORK="<network>" # dev, qa or test

Run the following command

running unit tests

make test

running integration tests

make integration