[lxc-devel] [lxd/master] Add some missing API extension checks

stgraber on Github lxc-bot at linuxcontainers.org
Tue Feb 20 21:40:55 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180220/4f6108df/attachment.bin>
-------------- next part --------------
From c98fa014a83955cb72e6be984483e7367e76be5a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 20 Feb 2018 16:26:04 -0500
Subject: [PATCH 1/2] client: Check API extension for storage
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 client/lxd_storage_pools.go   | 16 ++++++++++++++++
 client/lxd_storage_volumes.go | 20 ++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/client/lxd_storage_pools.go b/client/lxd_storage_pools.go
index 8e10288f1..8a93ae944 100644
--- a/client/lxd_storage_pools.go
+++ b/client/lxd_storage_pools.go
@@ -36,6 +36,10 @@ func (r *ProtocolLXD) GetStoragePoolNames() ([]string, error) {
 
 // GetStoragePools returns a list of StoragePool entries
 func (r *ProtocolLXD) GetStoragePools() ([]api.StoragePool, error) {
+	if !r.HasExtension("storage") {
+		return nil, fmt.Errorf("The server is missing the required \"storage\" API extension")
+	}
+
 	pools := []api.StoragePool{}
 
 	// Fetch the raw value
@@ -49,6 +53,10 @@ func (r *ProtocolLXD) GetStoragePools() ([]api.StoragePool, error) {
 
 // GetStoragePool returns a StoragePool entry for the provided pool name
 func (r *ProtocolLXD) GetStoragePool(name string) (*api.StoragePool, string, error) {
+	if !r.HasExtension("storage") {
+		return nil, "", fmt.Errorf("The server is missing the required \"storage\" API extension")
+	}
+
 	pool := api.StoragePool{}
 
 	// Fetch the raw value
@@ -81,6 +89,10 @@ func (r *ProtocolLXD) CreateStoragePool(pool api.StoragePoolsPost) error {
 
 // UpdateStoragePool updates the pool to match the provided StoragePool struct
 func (r *ProtocolLXD) UpdateStoragePool(name string, pool api.StoragePoolPut, ETag string) error {
+	if !r.HasExtension("storage") {
+		return fmt.Errorf("The server is missing the required \"storage\" API extension")
+	}
+
 	// Send the request
 	_, _, err := r.query("PUT", fmt.Sprintf("/storage-pools/%s", url.QueryEscape(name)), pool, ETag)
 	if err != nil {
@@ -92,6 +104,10 @@ func (r *ProtocolLXD) UpdateStoragePool(name string, pool api.StoragePoolPut, ET
 
 // DeleteStoragePool deletes a storage pool
 func (r *ProtocolLXD) DeleteStoragePool(name string) error {
+	if !r.HasExtension("storage") {
+		return fmt.Errorf("The server is missing the required \"storage\" API extension")
+	}
+
 	// Send the request
 	_, _, err := r.query("DELETE", fmt.Sprintf("/storage-pools/%s", url.QueryEscape(name)), nil, "")
 	if err != nil {
diff --git a/client/lxd_storage_volumes.go b/client/lxd_storage_volumes.go
index 1aa576e7f..26450ea19 100644
--- a/client/lxd_storage_volumes.go
+++ b/client/lxd_storage_volumes.go
@@ -36,6 +36,10 @@ func (r *ProtocolLXD) GetStoragePoolVolumeNames(pool string) ([]string, error) {
 
 // GetStoragePoolVolumes returns a list of StorageVolume entries for the provided pool
 func (r *ProtocolLXD) GetStoragePoolVolumes(pool string) ([]api.StorageVolume, error) {
+	if !r.HasExtension("storage") {
+		return nil, fmt.Errorf("The server is missing the required \"storage\" API extension")
+	}
+
 	volumes := []api.StorageVolume{}
 
 	// Fetch the raw value
@@ -49,6 +53,10 @@ func (r *ProtocolLXD) GetStoragePoolVolumes(pool string) ([]api.StorageVolume, e
 
 // GetStoragePoolVolume returns a StorageVolume entry for the provided pool and volume name
 func (r *ProtocolLXD) GetStoragePoolVolume(pool string, volType string, name string) (*api.StorageVolume, string, error) {
+	if !r.HasExtension("storage") {
+		return nil, "", fmt.Errorf("The server is missing the required \"storage\" API extension")
+	}
+
 	volume := api.StorageVolume{}
 
 	// Fetch the raw value
@@ -62,6 +70,10 @@ func (r *ProtocolLXD) GetStoragePoolVolume(pool string, volType string, name str
 
 // CreateStoragePoolVolume defines a new storage volume
 func (r *ProtocolLXD) CreateStoragePoolVolume(pool string, volume api.StorageVolumesPost) error {
+	if !r.HasExtension("storage") {
+		return fmt.Errorf("The server is missing the required \"storage\" API extension")
+	}
+
 	// Send the request
 	_, _, err := r.query("POST", fmt.Sprintf("/storage-pools/%s/volumes/%s", url.QueryEscape(pool), url.QueryEscape(volume.Type)), volume, "")
 	if err != nil {
@@ -73,6 +85,10 @@ func (r *ProtocolLXD) CreateStoragePoolVolume(pool string, volume api.StorageVol
 
 // UpdateStoragePoolVolume updates the volume to match the provided StoragePoolVolume struct
 func (r *ProtocolLXD) UpdateStoragePoolVolume(pool string, volType string, name string, volume api.StorageVolumePut, ETag string) error {
+	if !r.HasExtension("storage") {
+		return fmt.Errorf("The server is missing the required \"storage\" API extension")
+	}
+
 	// Send the request
 	_, _, err := r.query("PUT", fmt.Sprintf("/storage-pools/%s/volumes/%s/%s", url.QueryEscape(pool), url.QueryEscape(volType), url.QueryEscape(name)), volume, ETag)
 	if err != nil {
@@ -84,6 +100,10 @@ func (r *ProtocolLXD) UpdateStoragePoolVolume(pool string, volType string, name
 
 // DeleteStoragePoolVolume deletes a storage pool
 func (r *ProtocolLXD) DeleteStoragePoolVolume(pool string, volType string, name string) error {
+	if !r.HasExtension("storage") {
+		return fmt.Errorf("The server is missing the required \"storage\" API extension")
+	}
+
 	// Send the request
 	_, _, err := r.query("DELETE", fmt.Sprintf("/storage-pools/%s/volumes/%s/%s", url.QueryEscape(pool), url.QueryEscape(volType), url.QueryEscape(name)), nil, "")
 	if err != nil {

From d54e6eeb9c06dc217ef1c94d367d34e403835025 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 20 Feb 2018 16:29:50 -0500
Subject: [PATCH 2/2] client: Check API extension for network
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 client/lxd_networks.go | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/client/lxd_networks.go b/client/lxd_networks.go
index f48262341..8722e8329 100644
--- a/client/lxd_networks.go
+++ b/client/lxd_networks.go
@@ -10,6 +10,10 @@ import (
 
 // GetNetworkNames returns a list of network names
 func (r *ProtocolLXD) GetNetworkNames() ([]string, error) {
+	if !r.HasExtension("network") {
+		return nil, fmt.Errorf("The server is missing the required \"network\" API extension")
+	}
+
 	urls := []string{}
 
 	// Fetch the raw value
@@ -30,6 +34,10 @@ func (r *ProtocolLXD) GetNetworkNames() ([]string, error) {
 
 // GetNetworks returns a list of Network struct
 func (r *ProtocolLXD) GetNetworks() ([]api.Network, error) {
+	if !r.HasExtension("network") {
+		return nil, fmt.Errorf("The server is missing the required \"network\" API extension")
+	}
+
 	networks := []api.Network{}
 
 	// Fetch the raw value
@@ -43,6 +51,10 @@ func (r *ProtocolLXD) GetNetworks() ([]api.Network, error) {
 
 // GetNetwork returns a Network entry for the provided name
 func (r *ProtocolLXD) GetNetwork(name string) (*api.Network, string, error) {
+	if !r.HasExtension("network") {
+		return nil, "", fmt.Errorf("The server is missing the required \"network\" API extension")
+	}
+
 	network := api.Network{}
 
 	// Fetch the raw value
@@ -56,6 +68,10 @@ func (r *ProtocolLXD) GetNetwork(name string) (*api.Network, string, error) {
 
 // GetNetworkLeases returns a list of Network struct
 func (r *ProtocolLXD) GetNetworkLeases(name string) ([]api.NetworkLease, error) {
+	if !r.HasExtension("network_leases") {
+		return nil, fmt.Errorf("The server is missing the required \"network_leases\" API extension")
+	}
+
 	leases := []api.NetworkLease{}
 
 	// Fetch the raw value
@@ -84,6 +100,10 @@ func (r *ProtocolLXD) CreateNetwork(network api.NetworksPost) error {
 
 // UpdateNetwork updates the network to match the provided Network struct
 func (r *ProtocolLXD) UpdateNetwork(name string, network api.NetworkPut, ETag string) error {
+	if !r.HasExtension("network") {
+		return fmt.Errorf("The server is missing the required \"network\" API extension")
+	}
+
 	// Send the request
 	_, _, err := r.query("PUT", fmt.Sprintf("/networks/%s", url.QueryEscape(name)), network, ETag)
 	if err != nil {
@@ -95,6 +115,10 @@ func (r *ProtocolLXD) UpdateNetwork(name string, network api.NetworkPut, ETag st
 
 // RenameNetwork renames an existing network entry
 func (r *ProtocolLXD) RenameNetwork(name string, network api.NetworkPost) error {
+	if !r.HasExtension("network") {
+		return fmt.Errorf("The server is missing the required \"network\" API extension")
+	}
+
 	// Send the request
 	_, _, err := r.query("POST", fmt.Sprintf("/networks/%s", url.QueryEscape(name)), network, "")
 	if err != nil {
@@ -106,6 +130,10 @@ func (r *ProtocolLXD) RenameNetwork(name string, network api.NetworkPost) error
 
 // DeleteNetwork deletes an existing network
 func (r *ProtocolLXD) DeleteNetwork(name string) error {
+	if !r.HasExtension("network") {
+		return fmt.Errorf("The server is missing the required \"network\" API extension")
+	}
+
 	// Send the request
 	_, _, err := r.query("DELETE", fmt.Sprintf("/networks/%s", url.QueryEscape(name)), nil, "")
 	if err != nil {


More information about the lxc-devel mailing list