Best K6 code snippet using cloudapi.Apply
service_machines.go
Source:service_machines.go
1package cloudapi2import (3 "fmt"4 "strconv"5 "strings"6 "time"7 "github.com/joyent/gosdc/cloudapi"8 "github.com/joyent/gosdc/localservices"9)10// ListMachines returns a list of machines in the double11func (c *CloudAPI) ListMachines(filters map[string]string) ([]*cloudapi.Machine, error) {12 if err := c.ProcessFunctionHook(c, filters); err != nil {13 return nil, err14 }15 availableMachines := c.machines16 if filters != nil {17 for k, f := range filters {18 // check if valid filter19 if contains(machinesFilters, k) {20 machines := []*machine{}21 // filter from availableMachines and add to machines22 for _, m := range availableMachines {23 if k == "name" && m.Name == f {24 machines = append(machines, m)25 } else if k == "type" && m.Type == f {26 machines = append(machines, m)27 } else if k == "state" && m.State == f {28 machines = append(machines, m)29 } else if k == "image" && m.Image == f {30 machines = append(machines, m)31 } else if k == "memory" {32 i, err := strconv.Atoi(f)33 if err == nil && m.Memory == i {34 machines = append(machines, m)35 }36 } else if strings.HasPrefix(k, "tags.") {37 for t, v := range m.Tags {38 if t == k[strings.Index(k, ".")+1:] && v == f {39 machines = append(machines, m)40 }41 }42 }43 }44 availableMachines = machines45 }46 }47 }48 out := make([]*cloudapi.Machine, len(availableMachines))49 for i, machine := range availableMachines {50 out[i] = &machine.Machine51 }52 return out, nil53}54// CountMachines returns a count of machines the double knows about55func (c *CloudAPI) CountMachines() (int, error) {56 if err := c.ProcessFunctionHook(c); err != nil {57 return 0, err58 }59 return len(c.machines), nil60}61func (c *CloudAPI) getMachineWrapper(machineID string) (*machine, error) {62 if err := c.ProcessFunctionHook(c, machineID); err != nil {63 return nil, err64 }65 for _, machine := range c.machines {66 if machine.Id == machineID {67 return machine, nil68 }69 }70 return nil, fmt.Errorf("Machine %s not found", machineID)71}72// GetMachine gets a single machine by ID from the double73func (c *CloudAPI) GetMachine(machineID string) (*cloudapi.Machine, error) {74 wrapper, err := c.getMachineWrapper(machineID)75 if err != nil {76 return nil, err77 }78 return &wrapper.Machine, nil79}80// CreateMachine creates a new machine in the double. It will be running immediately.81func (c *CloudAPI) CreateMachine(name, pkg, image string, networks []string, metadata, tags map[string]string) (*cloudapi.Machine, error) {82 if err := c.ProcessFunctionHook(c, name, pkg, image); err != nil {83 return nil, err84 }85 machineID, err := localservices.NewUUID()86 if err != nil {87 return nil, err88 }89 mPkg, err := c.GetPackage(pkg)90 if err != nil {91 return nil, err92 }93 mImg, err := c.GetImage(image)94 if err != nil {95 return nil, err96 }97 mNetworks := []string{}98 for _, network := range networks {99 mNetwork, err := c.GetNetwork(network)100 if err != nil {101 return nil, err102 }103 mNetworks = append(mNetworks, mNetwork.Id)104 }105 publicIP := generatePublicIPAddress()106 newMachine := cloudapi.Machine{107 Id: machineID,108 Name: name,109 Type: mImg.Type,110 State: "running",111 Memory: mPkg.Memory,112 Disk: mPkg.Disk,113 IPs: []string{publicIP, generatePrivateIPAddress()},114 Created: time.Now().Format("2013-11-26T19:47:13.448Z"),115 Package: pkg,116 Image: image,117 Metadata: metadata,118 Tags: tags,119 PrimaryIP: publicIP,120 Networks: mNetworks,121 }122 nics := map[string]*cloudapi.NIC{}123 nicNetworks := map[string]string{}124 for i, network := range mNetworks {125 mac, err := localservices.NewMAC()126 if err != nil {127 return nil, err128 }129 nics[mac] = &cloudapi.NIC{130 IP: fmt.Sprintf("10.88.88.%d", i),131 MAC: mac,132 Primary: i == 0,133 Netmask: "255.255.255.0",134 Gateway: "10.88.88.2",135 State: cloudapi.NICStateRunning,136 Network: network,137 }138 nicNetworks[mac] = network139 }140 c.machines = append(c.machines, &machine{newMachine, nics, nicNetworks})141 return &newMachine, nil142}143// StopMachine changes a machine's status to "stopped"144func (c *CloudAPI) StopMachine(machineID string) error {145 if err := c.ProcessFunctionHook(c, machineID); err != nil {146 return err147 }148 for _, machine := range c.machines {149 if machine.Id == machineID {150 machine.State = "stopped"151 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")152 return nil153 }154 }155 return fmt.Errorf("Machine %s not found", machineID)156}157// StartMachine changes a machine's state to "running"158func (c *CloudAPI) StartMachine(machineID string) error {159 if err := c.ProcessFunctionHook(c, machineID); err != nil {160 return err161 }162 for _, machine := range c.machines {163 if machine.Id == machineID {164 machine.State = "running"165 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")166 return nil167 }168 }169 return fmt.Errorf("Machine %s not found", machineID)170}171// RebootMachine changes a machine's state to "running" and updates Updated172func (c *CloudAPI) RebootMachine(machineID string) error {173 if err := c.ProcessFunctionHook(c, machineID); err != nil {174 return err175 }176 for _, machine := range c.machines {177 if machine.Id == machineID {178 machine.State = "running"179 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")180 return nil181 }182 }183 return fmt.Errorf("Machine %s not found", machineID)184}185// ResizeMachine changes a machine's package to a new size. Unlike the real API,186// this method lets you downsize machines.187func (c *CloudAPI) ResizeMachine(machineID, packageName string) error {188 if err := c.ProcessFunctionHook(c, machineID, packageName); err != nil {189 return err190 }191 mPkg, err := c.GetPackage(packageName)192 if err != nil {193 return err194 }195 for _, machine := range c.machines {196 if machine.Id == machineID {197 machine.Package = packageName198 machine.Memory = mPkg.Memory199 machine.Disk = mPkg.Disk200 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")201 return nil202 }203 }204 return fmt.Errorf("Machine %s not found", machineID)205}206// RenameMachine changes a machine's name207func (c *CloudAPI) RenameMachine(machineID, newName string) error {208 if err := c.ProcessFunctionHook(c, machineID, newName); err != nil {209 return err210 }211 for _, machine := range c.machines {212 if machine.Id == machineID {213 machine.Name = newName214 machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z")215 return nil216 }217 }218 return fmt.Errorf("Machine %s not found", machineID)219}220// ListMachineFirewallRules returns a list of firewall rules that apply to the221// given machine222func (c *CloudAPI) ListMachineFirewallRules(machineID string) ([]*cloudapi.FirewallRule, error) {223 if err := c.ProcessFunctionHook(c, machineID); err != nil {224 return nil, err225 }226 fwRules := []*cloudapi.FirewallRule{}227 for _, r := range c.firewallRules {228 vm := "vm " + machineID229 if strings.Contains(r.Rule, vm) {230 fwRules = append(fwRules, r)231 }232 }233 return fwRules, nil234}235// EnableFirewallMachine enables the firewall for the given machine236func (c *CloudAPI) EnableFirewallMachine(machineID string) error {237 if err := c.ProcessFunctionHook(c, machineID); err != nil {238 return err239 }240 machine, err := c.GetMachine(machineID)241 if err != nil {242 return err243 }244 machine.FirewallEnabled = true245 return nil246}247// DisableFirewallMachine disables the firewall for the given machine248func (c *CloudAPI) DisableFirewallMachine(machineID string) error {249 if err := c.ProcessFunctionHook(c, machineID); err != nil {250 return err251 }252 machine, err := c.GetMachine(machineID)253 if err != nil {254 return err255 }256 machine.FirewallEnabled = false257 return nil258}259// DeleteMachine deletes the given machine from the double260func (c *CloudAPI) DeleteMachine(machineID string) error {261 if err := c.ProcessFunctionHook(c, machineID); err != nil {262 return err263 }264 for i, machine := range c.machines {265 if machine.Id == machineID {266 if machine.State == "stopped" {267 c.machines = append(c.machines[:i], c.machines[i+1:]...)268 return nil269 }270 return fmt.Errorf("Cannot Delete machine %s, machine is not stopped.", machineID)271 }272 }273 return fmt.Errorf("Machine %s not found", machineID)274}...
chargesverify.go
Source:chargesverify.go
1// RAINBOND, Application Management Platform2// Copyright (C) 2014-2017 Goodrain Co., Ltd.3// This program is free software: you can redistribute it and/or modify4// it under the terms of the GNU General Public License as published by5// the Free Software Foundation, either version 3 of the License, or6// (at your option) any later version. For any non-GPL usage of Rainbond,7// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.8// must be obtained first.9// This program is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.13// You should have received a copy of the GNU General Public License14// along with this program. If not, see <http://www.gnu.org/licenses/>.15package publiccloud16import (17 "fmt"18 "io/ioutil"19 "net/http"20 "os"21 "strings"22 "github.com/pquerna/ffjson/ffjson"23 "github.com/Sirupsen/logrus"24 "github.com/goodrain/rainbond/api/util"25 "github.com/goodrain/rainbond/db/model"26)27//ChargeSverify service Charge Sverify28func ChargeSverify(tenant *model.Tenants, quantity int, reason string) *util.APIHandleError {29 cloudAPI := os.Getenv("CLOUD_API")30 if cloudAPI == "" {31 cloudAPI = "http://api.goodrain.com"32 }33 regionName := os.Getenv("REGION_NAME")34 if regionName == "" {35 return util.CreateAPIHandleError(500, fmt.Errorf("region name must define in api by env REGION_NAME"))36 }37 reason = strings.Replace(reason, " ", "%20", -1)38 api := fmt.Sprintf("%s/openapi/console/v1/enterprises/%s/memory-apply?quantity=%d&tid=%s&reason=%s®ion=%s", cloudAPI, tenant.EID, quantity, tenant.UUID, reason, regionName)39 req, err := http.NewRequest("GET", api, nil)40 if err != nil {41 logrus.Error("create request cloud api error", err.Error())42 return util.CreateAPIHandleError(400, fmt.Errorf("create request cloud api error"))43 }44 res, err := http.DefaultClient.Do(req)45 if err != nil {46 logrus.Error("create request cloud api error", err.Error())47 return util.CreateAPIHandleError(400, fmt.Errorf("create request cloud api error"))48 }49 if res.Body != nil {50 defer res.Body.Close()51 rebody, _ := ioutil.ReadAll(res.Body)52 logrus.Debugf("read memory-apply response (%s)", string(rebody))53 var re = make(map[string]interface{})54 if err := ffjson.Unmarshal(rebody, &re); err == nil {55 if msg, ok := re["msg"]; ok {56 return util.CreateAPIHandleError(res.StatusCode, fmt.Errorf("%s", msg))57 }58 }59 }60 return util.CreateAPIHandleError(res.StatusCode, fmt.Errorf("none"))61}...
Apply
Using AI Code Generation
1import (2type CloudApiPlugin struct {3}4func main() {5 plugin.Start(new(CloudApiPlugin))6}7func (c *CloudApiPlugin) Run(context plugin.PluginContext, args []string) {8 if context.CLIContext().IsSet("help") || len(args) == 0 {9 c.Help()10 }11 if args[0] == "apply" {12 c.Apply(context)13 }14}15func (c *CloudApiPlugin) Apply(context plugin.PluginContext) {16 ui := ui.NewTerminalUI(context.CLIContext(), &i18n.T{}, terminal.NewStdUI())17 cmd := command.NewCmd(ui, context)18 cloudapi, err := cmd.CloudApi()19 if err != nil {20 ui.Failed(err.Error())21 }
Apply
Using AI Code Generation
1import (2func main() {3 client, err := compute.NewClient(4 if err != nil {5 panic(err)6 }7 instance, err := client.Instances.Apply(8 instance.Specification{
Apply
Using AI Code Generation
1import (2func main() {3 capi := cloudapi.NewCloudAPI()4 err := capi.Apply("policy.yaml")5 if err != nil {6 fmt.Println("Error:", err)7 }8}9import (10func main() {11 capi := cloudapi.NewCloudAPI()12 err := capi.Apply("policy.yaml")13 if err != nil {14 fmt.Println("Error:", err)15 }16}17import (18func main() {19 capi := cloudapi.NewCloudAPI()20 err := capi.Apply("policy.yaml")21 if err != nil {22 fmt.Println("Error:", err)23 }24}25import (26func main() {27 capi := cloudapi.NewCloudAPI()28 err := capi.Apply("policy.yaml")29 if err != nil {30 fmt.Println("Error:", err)31 }32}33import (34func main() {35 capi := cloudapi.NewCloudAPI()36 err := capi.Apply("policy.yaml")37 if err != nil {38 fmt.Println("Error:", err)39 }40}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!