Skip to content

Instantly share code, notes, and snippets.

@gservat
Last active December 18, 2017 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gservat/a1cd3feab3a197c5e6669f1c749d8b83 to your computer and use it in GitHub Desktop.
Save gservat/a1cd3feab3a197c5e6669f1c749d8b83 to your computer and use it in GitHub Desktop.
Non-working example to update a load balancer in Go
import (
"fmt"
"os"
"time"
rancher "github.com/rancher/go-rancher/v2"
log "github.com/sirupsen/logrus"
)
func createClient(rancherURL, accessKey, secretKey string) *rancher.RancherClient {
client, err := rancher.NewRancherClient(&rancher.ClientOpts{
Url: rancherURL,
AccessKey: accessKey,
SecretKey: secretKey,
Timeout: time.Second * 5,
})
if err != nil {
log.Errorf("Failed to create a client for rancher, error: %s", err)
os.Exit(1)
}
return client
}
func main() {
rancherClient := createClient("http://172.22.101.100/v2-beta", "<access key>", "<secret key>")
servicesRouter, err := rancherClient.LoadBalancerService.ById("1s8")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
portRule := rancher.PortRule{
Protocol: "http",
SourcePort: 8383,
TargetPort: 80,
ServiceId: "1s7",
Priority: 2,
Hostname: "",
}
servicesRouter.LbConfig.PortRules = append(servicesRouter.LbConfig.PortRules, portRule)
log.Info("updating the services router")
update, err := rancherClient.LoadBalancerService.ActionUpdate(servicesRouter)
if err != nil {
panic(err)
} else {
log.Info("update complete: ", update)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment