-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create network data source and get initial acceptance tests passing * Refactor FindFromGetAllObjects to use shared logic * Use XOA_POOL environment variable to parameterize integration / acceptance tests * Add docs for the data source * Fix verbose logging * Add stronger assertions and add tests for compare logic * Refactor setup code to be shared between the client and acceptance tests
- Loading branch information
Showing
14 changed files
with
502 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package client | ||
|
||
import "fmt" | ||
import ( | ||
"fmt" | ||
) | ||
|
||
type NotFound struct { | ||
Query XoObject | ||
Type string | ||
} | ||
|
||
func (e NotFound) Error() string { | ||
return fmt.Sprintf("Could not find %s with query: %+v", e.Type, e.Query) | ||
return fmt.Sprintf("Could not find %[1]T with query: %+[1]v", e.Query) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Network struct { | ||
Id string `json:"id"` | ||
NameLabel string `json:"name_label"` | ||
Bridge string | ||
PoolId string | ||
} | ||
|
||
func (net Network) Compare(obj map[string]interface{}) bool { | ||
id := obj["id"].(string) | ||
nameLabel := obj["name_label"].(string) | ||
poolId := obj["$poolId"].(string) | ||
if net.Id == id { | ||
return true | ||
} | ||
|
||
labelsMatch := false | ||
if net.NameLabel == nameLabel { | ||
labelsMatch = true | ||
} | ||
|
||
if net.PoolId == "" && labelsMatch { | ||
return true | ||
} else if net.PoolId == poolId && labelsMatch { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (net Network) New(obj map[string]interface{}) XoObject { | ||
id := obj["id"].(string) | ||
poolId := obj["$poolId"].(string) | ||
nameLabel := obj["name_label"].(string) | ||
bridge := obj["bridge"].(string) | ||
return Network{ | ||
Id: id, | ||
Bridge: bridge, | ||
PoolId: poolId, | ||
NameLabel: nameLabel, | ||
} | ||
} | ||
|
||
func (c *Client) CreateNetwork(netReq Network) (*Network, error) { | ||
var id string | ||
params := map[string]interface{}{ | ||
"pool": netReq.PoolId, | ||
"name": netReq.NameLabel, | ||
} | ||
|
||
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) | ||
err := c.Call(ctx, "network.create", params, &id) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return c.GetNetwork(Network{Id: id}) | ||
} | ||
|
||
func (c *Client) GetNetwork(netReq Network) (*Network, error) { | ||
obj, err := c.FindFromGetAllObjects(netReq) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if _, ok := obj.([]interface{}); ok { | ||
return nil, errors.New("Your query returned more than one result. Use `pool_id` or other attributes to filter the result down to a single network") | ||
} | ||
|
||
net := obj.(Network) | ||
return &net, nil | ||
} | ||
|
||
func (c *Client) GetNetworks() ([]Network, error) { | ||
var response map[string]Network | ||
err := c.GetAllObjectsOfType(Network{}, &response) | ||
|
||
nets := make([]Network, 0, len(response)) | ||
for _, net := range response { | ||
nets = append(nets, net) | ||
} | ||
return nets, err | ||
} | ||
|
||
func (c *Client) DeleteNetwork(id string) error { | ||
var success bool | ||
params := map[string]interface{}{ | ||
"id": id, | ||
} | ||
|
||
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) | ||
err := c.Call(ctx, "network.delete", params, &success) | ||
|
||
return err | ||
} | ||
|
||
func RemoveNetworksWithNamePrefix(prefix string) func(string) error { | ||
return func(_ string) error { | ||
c, err := NewClient(GetConfigFromEnv()) | ||
if err != nil { | ||
return fmt.Errorf("error getting client: %s", err) | ||
} | ||
|
||
nets, err := c.GetNetworks() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, net := range nets { | ||
if strings.HasPrefix(net.NameLabel, prefix) { | ||
log.Printf("[DEBUG] Deleting network: %v\n", net) | ||
err = c.DeleteNetwork(net.Id) | ||
|
||
if err != nil { | ||
log.Printf("error destroying network `%s` during sweep: %v", net.NameLabel, err) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package client | ||
|
||
import "testing" | ||
|
||
var testNetworkName string = integrationTestPrefix + "network" | ||
|
||
func TestNetworkCompare(t *testing.T) { | ||
|
||
nameLabel := "network label" | ||
poolId := "pool id" | ||
cases := []struct { | ||
net Network | ||
result bool | ||
obj map[string]interface{} | ||
}{ | ||
{ | ||
net: Network{ | ||
NameLabel: nameLabel, | ||
}, | ||
result: true, | ||
obj: map[string]interface{}{ | ||
"id": "355ee47d-ff4c-4924-3db2-fd86ae629676", | ||
"name_label": nameLabel, | ||
"$poolId": "355ee47d-ff4c-4924-3db2-fd86ae629676", | ||
}, | ||
}, | ||
{ | ||
net: Network{ | ||
NameLabel: nameLabel, | ||
PoolId: poolId, | ||
}, | ||
result: false, | ||
obj: map[string]interface{}{ | ||
"id": "355ee47d-ff4c-4924-3db2-fd86ae629676", | ||
"name_label": nameLabel, | ||
"$poolId": "355ee47d-ff4c-4924-3db2-fd86ae629676", | ||
}, | ||
}, | ||
{ | ||
net: Network{ | ||
NameLabel: nameLabel, | ||
PoolId: poolId, | ||
}, | ||
result: true, | ||
obj: map[string]interface{}{ | ||
"id": "355ee47d-ff4c-4924-3db2-fd86ae629676", | ||
"name_label": nameLabel, | ||
"$poolId": poolId, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range cases { | ||
net := test.net | ||
obj := test.obj | ||
result := test.result | ||
|
||
if net.Compare(obj) != result { | ||
t.Errorf("expected network `%+v` to Compare '%t' with object `%v`", net, result, obj) | ||
} | ||
} | ||
} | ||
|
||
func TestGetNetwork(t *testing.T) { | ||
c, err := NewClient(GetConfigFromEnv()) | ||
|
||
if err != nil { | ||
t.Fatalf("failed to create client with error: %v", err) | ||
} | ||
|
||
net, err := c.GetNetwork(Network{ | ||
NameLabel: testNetworkName, | ||
}) | ||
|
||
if err != nil { | ||
t.Fatalf("failed to retrieve network `%s` with error: %v", testNetworkName, err) | ||
} | ||
|
||
if net == nil { | ||
t.Fatalf("should have received network, instead received nil") | ||
} | ||
|
||
if net.NameLabel != testNetworkName { | ||
t.Errorf("expected network name_label `%s` to match `%s`", net.NameLabel, testNetworkName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package client | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func CreateNetwork() error { | ||
|
||
c, err := NewClient(GetConfigFromEnv()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = c.CreateNetwork(Network{ | ||
NameLabel: testNetworkName, | ||
PoolId: accTestPool.Id, | ||
}) | ||
return err | ||
} | ||
|
||
var integrationTestPrefix string = "xenorchestra-client-" | ||
var accTestPool Pool | ||
|
||
func TestMain(m *testing.M) { | ||
FindPoolForTests(&accTestPool) | ||
CreateNetwork() | ||
code := m.Run() | ||
|
||
RemoveNetworksWithNamePrefix(integrationTestPrefix)("") | ||
|
||
os.Exit(code) | ||
} |
Oops, something went wrong.