Skip to content

Commit

Permalink
Added comments and renamed things to pass golint
Browse files Browse the repository at this point in the history
  • Loading branch information
dandyrow committed Oct 21, 2022
1 parent 3e3fbf4 commit aeb7364
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,13 @@ func main() {
log.Printf("Network interface %s configuration: %s", iface, exitStatus)

case "createNetwork":
config, err := proxmox.NewConfigNetworkFromJson(GetConfig(*fConfigFile))
config, err := proxmox.NewConfigNetworkFromJSON(GetConfig(*fConfigFile))
failError(err)
failError(config.CreateNetwork(c))
log.Printf("Network %s has been created\n", config.Iface)

case "updateNetwork":
config, err := proxmox.NewConfigNetworkFromJson(GetConfig(*fConfigFile))
config, err := proxmox.NewConfigNetworkFromJSON(GetConfig(*fConfigFile))
failError(err)
failError(config.UpdateNetwork(c))
log.Printf("Network %s has been updated\n", config.Iface)
Expand Down
30 changes: 28 additions & 2 deletions proxmox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,11 @@ func (c *Client) DeleteStorage(id string) error {
}

// Network

// GetNetworkList gets a json encoded list of currently configured network interfaces on the
// passed in node. The typeFilter parameter can be used to filter by interface type. Pass in
// the empty string "" for typeFilter to list all network interfaces on the node.
// It returns the body from the API response and any HTTP error the API returns.
func (c *Client) GetNetworkList(node string, typeFilter string) (exitStatus string, err error) {
url := fmt.Sprintf("/nodes/%s/network", node)
if typeFilter != "" {
Expand All @@ -1783,35 +1788,50 @@ func (c *Client) GetNetworkList(node string, typeFilter string) (exitStatus stri
return
}

// GetNetworkInterface gets a json encoded object containing the configuration of the network
// interface with the name passed in as iface from the passed in node.
// It returns the body from the API response and any HTTP error the API returns.
func (c *Client) GetNetworkInterface(node string, iface string) (exitStatus string, err error) {
url := fmt.Sprintf("/nodes/%s/network/%s", node, iface)
resp, err := c.session.Get(url, nil, nil)
exitStatus = c.HandleTaskError(resp)
return
}

// CreateNetwork creates a network with the configuration of the passed in parameters
// on the passed in node.
// It returns the body from the API response and any HTTP error the API returns.
func (c *Client) CreateNetwork(node string, params map[string]interface{}) (exitStatus string, err error) {
url := fmt.Sprintf("/nodes/%s/network", node)
return c.CreateItemReturnStatus(params, url)
}

// UpdateNetwork updates the network corresponding to the passed in interface name on the passed
// in node with the configuration in the passed in parameters.
// It returns the body from the API response and any HTTP error the API returns.
func (c *Client) UpdateNetwork(node string, iface string, params map[string]interface{}) (exitStatus string, err error) {
url := fmt.Sprintf("/nodes/%s/network/%s", node, iface)
return c.UpdateItemReturnStatus(params, url)
}

// DeleteNetwork deletes the network with the passed in iface name on the passed in node.
// It returns the body from the API response and any HTTP error the API returns.
func (c *Client) DeleteNetwork(node string, iface string) (exitStatus string, err error) {
url := fmt.Sprintf("/nodes/%s/network/%s", node, iface)
resp, err := c.session.Delete(url, nil, nil)
exitStatus = c.HandleTaskError(resp)
return
}

// ApplyNetwork applies the pending network configuration on the passed in node.
// It returns the body from the API response and any HTTP error the API returns.
func (c Client) ApplyNetwork(node string) (exitStatus string, err error) {
url := fmt.Sprintf("/nodes/%s/network", node)
return c.UpdateItemWithTask(nil, url)
}

// RevertNetwork reverts the pending network configuration on the passed in node.
// It returns the body from the API response and any HTTP error the API returns.
func (c *Client) RevertNetwork(node string) (exitStatus string, err error) {
url := fmt.Sprintf("/nodes/%s/network", node)
return c.DeleteUrlWithTask(url)
Expand Down Expand Up @@ -1859,6 +1879,8 @@ func (c *Client) CreateItem(Params map[string]interface{}, url string) (err erro
return
}

// CreateItemReturnStatus creates an item on the Proxmox API.
// It returns the body of the HTTP response and any HTTP error occurred during the request.
func (c *Client) CreateItemReturnStatus(params map[string]interface{}, url string) (exitStatus string, err error) {
reqbody := ParamsToBody(params)
resp, err := c.session.Post(url, nil, nil, &reqbody)
Expand All @@ -1883,6 +1905,8 @@ func (c *Client) UpdateItem(Params map[string]interface{}, url string) (err erro
return
}

// UpdateItemReturnStatus updates an item on the Proxmox API.
// It returns the body of the HTTP response and any HTTP error occurred during the request.
func (c *Client) UpdateItemReturnStatus(params map[string]interface{}, url string) (exitStatus string, err error) {
reqbody := ParamsToBody(params)
resp, err := c.session.Put(url, nil, nil, &reqbody)
Expand Down Expand Up @@ -1921,7 +1945,8 @@ func (c *Client) GetItemList(url string) (list map[string]interface{}, err error
return
}

// Close task responce
// HandleTaskError reads the body from the passed in HTTP response and closes it.
// It returns the body of the passed in HTTP response.
func (c *Client) HandleTaskError(resp *http.Response) (exitStatus string) {
defer resp.Body.Close()
// This might not work if we never got a body. We'll ignore errors in trying to read,
Expand All @@ -1930,7 +1955,8 @@ func (c *Client) HandleTaskError(resp *http.Response) (exitStatus string) {
return string(b)
}

// Check if the proxmox task has been completed
// CheckTask polls the API to check if the Proxmox task has been completed.
// It returns the body of the HTTP response and any HTTP error occurred during the request.
func (c *Client) CheckTask(resp *http.Response) (exitStatus string, err error) {
taskResponse, err := ResponseJSON(resp)
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion proxmox/config_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
)

// ConfigNetwork maps go variables to API parameters.
type ConfigNetwork struct {
Iface string `json:"iface,omitempty"`
Node string `json:"node,omitempty"`
Expand Down Expand Up @@ -36,19 +37,28 @@ type ConfigNetwork struct {
VlanRawDevice string `json:"vlan-raw-device,omitempty"`
}

func NewConfigNetworkFromJson(input []byte) (config *ConfigNetwork, err error) {
// NewConfigNetworkFromJSON takes in a byte array from a json encoded network
// configuration and stores it in config.
// It returns the newly created config with the passed in configuration stored
// and an error if one occurs unmarshalling the input data.
func NewConfigNetworkFromJSON(input []byte) (config *ConfigNetwork, err error) {
config = &ConfigNetwork{}
err = json.Unmarshal([]byte(input), config)
return
}

// MapToAPIParams converts the stored config into a parameter map to be
// sent to the API.
func (config ConfigNetwork) MapToAPIParams() map[string]interface{} {
params, _ := json.Marshal(&config)
var paramMap map[string]interface{}
json.Unmarshal(params, &paramMap)
return paramMap
}

// CreateNetwork creates a network on the Proxmox host with the stored
// config.
// It returns an error if the creation of the network fails.
func (config ConfigNetwork) CreateNetwork(client *Client) (err error) {
paramMap := config.MapToAPIParams()

Expand All @@ -60,6 +70,10 @@ func (config ConfigNetwork) CreateNetwork(client *Client) (err error) {
return
}


// UpdateNetwork updates a network on the Proxmox host with the stored
// config.
// It returns an error if updating the network fails.
func (config ConfigNetwork) UpdateNetwork(client *Client) (err error) {
paramMap := config.MapToAPIParams()

Expand Down

0 comments on commit aeb7364

Please sign in to comment.