Skip to content

Commit

Permalink
Merge pull request #12764 from MusicDin/fix/extension-checks
Browse files Browse the repository at this point in the history
Replace deprecated HasExtension checks
  • Loading branch information
tomponline authored Jan 23, 2024
2 parents 1f42fd0 + cd42b6f commit a890222
Show file tree
Hide file tree
Showing 16 changed files with 744 additions and 514 deletions.
12 changes: 7 additions & 5 deletions client/lxd_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ func (r *ProtocolLXD) CreateCertificate(certificate api.CertificatesPost) error

// UpdateCertificate updates the certificate definition.
func (r *ProtocolLXD) UpdateCertificate(fingerprint string, certificate api.CertificatePut, ETag string) error {
if !r.HasExtension("certificate_update") {
return fmt.Errorf("The server is missing the required \"certificate_update\" API extension")
err := r.CheckExtension("certificate_update")
if err != nil {
return err
}

// Send the request
_, _, err := r.query("PUT", fmt.Sprintf("/certificates/%s", url.PathEscape(fingerprint)), certificate, ETag)
_, _, err = r.query("PUT", fmt.Sprintf("/certificates/%s", url.PathEscape(fingerprint)), certificate, ETag)
if err != nil {
return err
}
Expand All @@ -88,8 +89,9 @@ func (r *ProtocolLXD) DeleteCertificate(fingerprint string) error {

// CreateCertificateToken requests a certificate add token.
func (r *ProtocolLXD) CreateCertificateToken(certificate api.CertificatesPost) (Operation, error) {
if !r.HasExtension("certificate_token") {
return nil, fmt.Errorf("The server is missing the required \"certificate_token\" API extension")
err := r.CheckExtension("certificate_token")
if err != nil {
return nil, err
}

if !certificate.Token {
Expand Down
124 changes: 72 additions & 52 deletions client/lxd_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
//
// If this client is not trusted, the password must be supplied.
func (r *ProtocolLXD) GetCluster() (*api.Cluster, string, error) {
if !r.HasExtension("clustering") {
return nil, "", fmt.Errorf("The server is missing the required \"clustering\" API extension")
err := r.CheckExtension("clustering")
if err != nil {
return nil, "", err
}

cluster := &api.Cluster{}
Expand All @@ -25,13 +26,15 @@ func (r *ProtocolLXD) GetCluster() (*api.Cluster, string, error) {

// UpdateCluster requests to bootstrap a new cluster or join an existing one.
func (r *ProtocolLXD) UpdateCluster(cluster api.ClusterPut, ETag string) (Operation, error) {
if !r.HasExtension("clustering") {
return nil, fmt.Errorf("The server is missing the required \"clustering\" API extension")
err := r.CheckExtension("clustering")
if err != nil {
return nil, err
}

if cluster.ServerAddress != "" || cluster.ClusterPassword != "" || len(cluster.MemberConfig) > 0 {
if !r.HasExtension("clustering_join") {
return nil, fmt.Errorf("The server is missing the required \"clustering_join\" API extension")
err := r.CheckExtension("clustering_join")
if err != nil {
return nil, err
}
}

Expand All @@ -46,16 +49,17 @@ func (r *ProtocolLXD) UpdateCluster(cluster api.ClusterPut, ETag string) (Operat
// DeleteClusterMember makes the given member leave the cluster (gracefully or not,
// depending on the force flag).
func (r *ProtocolLXD) DeleteClusterMember(name string, force bool) error {
if !r.HasExtension("clustering") {
return fmt.Errorf("The server is missing the required \"clustering\" API extension")
err := r.CheckExtension("clustering")
if err != nil {
return err
}

params := ""
if force {
params += "?force=1"
}

_, _, err := r.query("DELETE", fmt.Sprintf("/cluster/members/%s%s", name, params), nil, "")
_, _, err = r.query("DELETE", fmt.Sprintf("/cluster/members/%s%s", name, params), nil, "")
if err != nil {
return err
}
Expand All @@ -65,14 +69,15 @@ func (r *ProtocolLXD) DeleteClusterMember(name string, force bool) error {

// GetClusterMemberNames returns the URLs of the current members in the cluster.
func (r *ProtocolLXD) GetClusterMemberNames() ([]string, error) {
if !r.HasExtension("clustering") {
return nil, fmt.Errorf("The server is missing the required \"clustering\" API extension")
err := r.CheckExtension("clustering")
if err != nil {
return nil, err
}

// Fetch the raw URL values.
urls := []string{}
baseURL := "/cluster/members"
_, err := r.queryStruct("GET", baseURL, nil, "", &urls)
_, err = r.queryStruct("GET", baseURL, nil, "", &urls)
if err != nil {
return nil, err
}
Expand All @@ -83,12 +88,13 @@ func (r *ProtocolLXD) GetClusterMemberNames() ([]string, error) {

// GetClusterMembers returns the current members of the cluster.
func (r *ProtocolLXD) GetClusterMembers() ([]api.ClusterMember, error) {
if !r.HasExtension("clustering") {
return nil, fmt.Errorf("The server is missing the required \"clustering\" API extension")
err := r.CheckExtension("clustering")
if err != nil {
return nil, err
}

members := []api.ClusterMember{}
_, err := r.queryStruct("GET", "/cluster/members?recursion=1", nil, "", &members)
_, err = r.queryStruct("GET", "/cluster/members?recursion=1", nil, "", &members)
if err != nil {
return nil, err
}
Expand All @@ -98,8 +104,9 @@ func (r *ProtocolLXD) GetClusterMembers() ([]api.ClusterMember, error) {

// GetClusterMember returns information about the given member.
func (r *ProtocolLXD) GetClusterMember(name string) (*api.ClusterMember, string, error) {
if !r.HasExtension("clustering") {
return nil, "", fmt.Errorf("The server is missing the required \"clustering\" API extension")
err := r.CheckExtension("clustering")
if err != nil {
return nil, "", err
}

member := api.ClusterMember{}
Expand All @@ -113,18 +120,20 @@ func (r *ProtocolLXD) GetClusterMember(name string) (*api.ClusterMember, string,

// UpdateClusterMember updates information about the given member.
func (r *ProtocolLXD) UpdateClusterMember(name string, member api.ClusterMemberPut, ETag string) error {
if !r.HasExtension("clustering_edit_roles") {
return fmt.Errorf("The server is missing the required \"clustering_edit_roles\" API extension")
err := r.CheckExtension("clustering_edit_roles")
if err != nil {
return err
}

if member.FailureDomain != "" {
if !r.HasExtension("clustering_failure_domains") {
return fmt.Errorf("The server is missing the required \"clustering_failure_domains\" API extension")
err := r.CheckExtension("clustering_failure_domains")
if err != nil {
return err
}
}

// Send the request
_, _, err := r.query("PUT", fmt.Sprintf("/cluster/members/%s", name), member, ETag)
_, _, err = r.query("PUT", fmt.Sprintf("/cluster/members/%s", name), member, ETag)
if err != nil {
return err
}
Expand All @@ -134,11 +143,12 @@ func (r *ProtocolLXD) UpdateClusterMember(name string, member api.ClusterMemberP

// RenameClusterMember changes the name of an existing member.
func (r *ProtocolLXD) RenameClusterMember(name string, member api.ClusterMemberPost) error {
if !r.HasExtension("clustering") {
return fmt.Errorf("The server is missing the required \"clustering\" API extension")
err := r.CheckExtension("clustering")
if err != nil {
return err
}

_, _, err := r.query("POST", fmt.Sprintf("/cluster/members/%s", name), member, "")
_, _, err = r.query("POST", fmt.Sprintf("/cluster/members/%s", name), member, "")
if err != nil {
return err
}
Expand All @@ -148,8 +158,9 @@ func (r *ProtocolLXD) RenameClusterMember(name string, member api.ClusterMemberP

// CreateClusterMember generates a join token to add a cluster member.
func (r *ProtocolLXD) CreateClusterMember(member api.ClusterMembersPost) (Operation, error) {
if !r.HasExtension("clustering_join_token") {
return nil, fmt.Errorf("The server is missing the required \"clustering_join_token\" API extension")
err := r.CheckExtension("clustering_join_token")
if err != nil {
return nil, err
}

op, _, err := r.queryOperation("POST", "/cluster/members", member, "", true)
Expand All @@ -162,11 +173,12 @@ func (r *ProtocolLXD) CreateClusterMember(member api.ClusterMembersPost) (Operat

// UpdateClusterCertificate updates the cluster certificate for every node in the cluster.
func (r *ProtocolLXD) UpdateClusterCertificate(certs api.ClusterCertificatePut, ETag string) error {
if !r.HasExtension("clustering_update_cert") {
return fmt.Errorf("The server is missing the required \"clustering_update_cert\" API extension")
err := r.CheckExtension("clustering_update_cert")
if err != nil {
return err
}

_, _, err := r.query("PUT", "/cluster/certificate", certs, ETag)
_, _, err = r.query("PUT", "/cluster/certificate", certs, ETag)
if err != nil {
return err
}
Expand All @@ -193,8 +205,9 @@ func (r *ProtocolLXD) GetClusterMemberState(name string) (*api.ClusterMemberStat

// UpdateClusterMemberState evacuates or restores a cluster member.
func (r *ProtocolLXD) UpdateClusterMemberState(name string, state api.ClusterMemberStatePost) (Operation, error) {
if !r.HasExtension("clustering_evacuation") {
return nil, fmt.Errorf("The server is missing the required \"clustering_evacuation\" API extension")
err := r.CheckExtension("clustering_evacuation")
if err != nil {
return nil, err
}

op, _, err := r.queryOperation("POST", fmt.Sprintf("/cluster/members/%s/state", name), state, "", true)
Expand All @@ -207,13 +220,14 @@ func (r *ProtocolLXD) UpdateClusterMemberState(name string, state api.ClusterMem

// GetClusterGroups returns the cluster groups.
func (r *ProtocolLXD) GetClusterGroups() ([]api.ClusterGroup, error) {
if !r.HasExtension("clustering_groups") {
return nil, fmt.Errorf("The server is missing the required \"clustering_groups\" API extension")
err := r.CheckExtension("clustering_groups")
if err != nil {
return nil, err
}

groups := []api.ClusterGroup{}

_, err := r.queryStruct("GET", "/cluster/groups?recursion=1", nil, "", &groups)
_, err = r.queryStruct("GET", "/cluster/groups?recursion=1", nil, "", &groups)
if err != nil {
return nil, err
}
Expand All @@ -223,13 +237,14 @@ func (r *ProtocolLXD) GetClusterGroups() ([]api.ClusterGroup, error) {

// GetClusterGroupNames returns the cluster group names.
func (r *ProtocolLXD) GetClusterGroupNames() ([]string, error) {
if !r.HasExtension("clustering_groups") {
return nil, fmt.Errorf("The server is missing the required \"clustering_groups\" API extension")
err := r.CheckExtension("clustering_groups")
if err != nil {
return nil, err
}

urls := []string{}

_, err := r.queryStruct("GET", "/cluster/groups", nil, "", &urls)
_, err = r.queryStruct("GET", "/cluster/groups", nil, "", &urls)
if err != nil {
return nil, err
}
Expand All @@ -240,11 +255,12 @@ func (r *ProtocolLXD) GetClusterGroupNames() ([]string, error) {

// RenameClusterGroup changes the name of an existing cluster group.
func (r *ProtocolLXD) RenameClusterGroup(name string, group api.ClusterGroupPost) error {
if !r.HasExtension("clustering_groups") {
return fmt.Errorf("The server is missing the required \"clustering_groups\" API extension")
err := r.CheckExtension("clustering_groups")
if err != nil {
return err
}

_, _, err := r.query("POST", fmt.Sprintf("/cluster/groups/%s", name), group, "")
_, _, err = r.query("POST", fmt.Sprintf("/cluster/groups/%s", name), group, "")
if err != nil {
return err
}
Expand All @@ -254,11 +270,12 @@ func (r *ProtocolLXD) RenameClusterGroup(name string, group api.ClusterGroupPost

// CreateClusterGroup creates a new cluster group.
func (r *ProtocolLXD) CreateClusterGroup(group api.ClusterGroupsPost) error {
if !r.HasExtension("clustering_groups") {
return fmt.Errorf("The server is missing the required \"clustering_groups\" API extension")
err := r.CheckExtension("clustering_groups")
if err != nil {
return err
}

_, _, err := r.query("POST", "/cluster/groups", group, "")
_, _, err = r.query("POST", "/cluster/groups", group, "")
if err != nil {
return err
}
Expand All @@ -268,11 +285,12 @@ func (r *ProtocolLXD) CreateClusterGroup(group api.ClusterGroupsPost) error {

// DeleteClusterGroup deletes an existing cluster group.
func (r *ProtocolLXD) DeleteClusterGroup(name string) error {
if !r.HasExtension("clustering_groups") {
return fmt.Errorf("The server is missing the required \"clustering_groups\" API extension")
err := r.CheckExtension("clustering_groups")
if err != nil {
return err
}

_, _, err := r.query("DELETE", fmt.Sprintf("/cluster/groups/%s", name), nil, "")
_, _, err = r.query("DELETE", fmt.Sprintf("/cluster/groups/%s", name), nil, "")
if err != nil {
return err
}
Expand All @@ -282,12 +300,13 @@ func (r *ProtocolLXD) DeleteClusterGroup(name string) error {

// UpdateClusterGroup updates information about the given cluster group.
func (r *ProtocolLXD) UpdateClusterGroup(name string, group api.ClusterGroupPut, ETag string) error {
if !r.HasExtension("clustering_groups") {
return fmt.Errorf("The server is missing the required \"clustering_groups\" API extension")
err := r.CheckExtension("clustering_groups")
if err != nil {
return err
}

// Send the request
_, _, err := r.query("PUT", fmt.Sprintf("/cluster/groups/%s", name), group, ETag)
_, _, err = r.query("PUT", fmt.Sprintf("/cluster/groups/%s", name), group, ETag)
if err != nil {
return err
}
Expand All @@ -297,8 +316,9 @@ func (r *ProtocolLXD) UpdateClusterGroup(name string, group api.ClusterGroupPut,

// GetClusterGroup returns information about the given cluster group.
func (r *ProtocolLXD) GetClusterGroup(name string) (*api.ClusterGroup, string, error) {
if !r.HasExtension("clustering_groups") {
return nil, "", fmt.Errorf("The server is missing the required \"clustering_groups\" API extension")
err := r.CheckExtension("clustering_groups")
if err != nil {
return nil, "", err
}

group := api.ClusterGroup{}
Expand Down
Loading

0 comments on commit a890222

Please sign in to comment.