Skip to content

Instantly share code, notes, and snippets.

@lubronzhan
Created August 7, 2024 19:22
Show Gist options
  • Save lubronzhan/6a75168e69a6718a5f5d61a3ad39db69 to your computer and use it in GitHub Desktop.
Save lubronzhan/6a75168e69a6718a5f5d61a3ad39db69 to your computer and use it in GitHub Desktop.
update avi cert
package main
import (
"errors"
"flag"
"fmt"
"net/http"
"os"
"github.com/vmware/alb-sdk/go/clients"
"github.com/vmware/alb-sdk/go/session"
"k8s.io/utils/ptr"
)
const (
CAName = "System-Default-Root-CA"
)
func main() {
username := flag.String("username", "", "a string")
password := flag.String("password", "", "a string")
controllerIP := flag.String("ip", "", "a string")
flag.Parse()
c, err := NewAviClient(*username, *password, *controllerIP)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ca, err := c.SSLKeyAndCertificate.GetByName(CAName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
sysConfig, err := c.SystemConfiguration.Get("")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(*ca.URL)
sysConfig.UUID = ptr.To("")
sysConfig.PortalConfiguration.SslkeyandcertificateRefs = []string{*ca.URL}
_, err = c.SystemConfiguration.Update(sysConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// NewAviClient creates an Client
func NewAviClient(username, password, controllerIP string) (*clients.AviClient, error) {
// Initialize transport
var transport *http.Transport
options := []func(*session.AviSession) error{
session.SetPassword(password),
session.SetTransport(transport),
session.SetTenant(username),
session.SetVersion(""),
session.SetInsecure,
}
client, err := clients.NewAviClient(controllerIP, username, options...)
if err != nil {
return nil, err
}
version, err := client.AviSession.GetControllerVersion()
if err != nil {
return nil, errors.New("cannot get avi controller version")
}
options = []func(*session.AviSession) error{
session.SetPassword(password),
session.SetTransport(transport),
session.SetTenant(username),
session.SetVersion(version),
session.SetInsecure,
}
client, err = clients.NewAviClient(controllerIP, username, options...)
if err != nil {
return nil, err
}
return client, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment