Skip to content

Commit

Permalink
lxc/client: Add GetMetadataConfiguration method
Browse files Browse the repository at this point in the history
Signed-off-by: Kadin Sayani <[email protected]>
  • Loading branch information
kadinsayani committed Sep 17, 2024
1 parent 217bacb commit 33f0800
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type InstanceServer interface {
ImageServer

// Server functions
GetMetadataConfiguration() (config *map[string]any, err error)
GetMetrics() (metrics string, err error)
GetServer() (server *api.Server, ETag string, err error)
GetServerResources() (resources *api.Resources, err error)
Expand Down
48 changes: 48 additions & 0 deletions client/lxd_server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lxd

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -186,3 +187,50 @@ func (r *ProtocolLXD) GetMetrics() (string, error) {

return string(content), nil
}

// GetMetadataConfiguration returns metadata configuration for a server.
func (r *ProtocolLXD) GetMetadataConfiguration() (*map[string]any, error) {
// Check that the server supports it.
err := r.CheckExtension("metadata_configuration")
if err != nil {
return nil, err
}

// Prepare the request.
requestURL, err := r.setQueryAttributes(fmt.Sprintf("%s/1.0/metadata/configuration", r.httpBaseURL.String()))
if err != nil {
return nil, err
}

req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return nil, err
}

// Send the request.
resp, err := r.DoHTTP(req)
if err != nil {
return nil, err
}

defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Bad HTTP status: %d", resp.StatusCode)
}

// Get the content.
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

// Unmarshal content.
config := make(map[string]any)
err = json.Unmarshal(content, &config)
if err != nil {
return nil, err
}

return &config, nil
}

0 comments on commit 33f0800

Please sign in to comment.