diff --git a/README.md b/README.md index 781925c..f5196dc 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,6 @@ API documentation is also available at the [VPC page](https://my.selectel.ru/vpc You can use this library to work with the following objects of the Selectel VPC API: * [capabilities](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/capabilities) -* [cross region subnets](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/crossregionsubnets) * [floating ips](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/floatingips) * [keypairs](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/keypairs) * [licenses](https://godoc.org/github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/licenses) diff --git a/selvpcclient/resell/v2/crossregionsubnets/doc.go b/selvpcclient/resell/v2/crossregionsubnets/doc.go deleted file mode 100644 index 5631ed7..0000000 --- a/selvpcclient/resell/v2/crossregionsubnets/doc.go +++ /dev/null @@ -1,56 +0,0 @@ -/* -Package crossregionsubnets provides the ability to retrieve and manage cross-region subnets -through the Resell v2 API. - -Example of getting a single cross-region subnet referenced by its id - - crossRegionSubnet, _, err := crossregionsubnets.Get(context, resellClient, crossRegionSubnetID) - if err != nil { - log.Fatal(err) - } - fmt.Println(crossRegionSubnet) - -Example of getting all cross-region subnets - - allCrossRegionSubnets, _, err := crossregionsubnets.List(ctx, resellClient, crossregionsubnets.ListOpts{}) - if err != nil { - log.Fatal(err) - } - for _, crossRegionSubnet := range allCrossRegionSubnets { - fmt.Println(crossRegionSubnet) - } - -Example of creating cross-region subnets - - createOpts := crossregionsubnets.CrossRegionSubnetOpts{ - CrossRegionSubnets: []crossregionsubnets.CrossRegionSubnetOpt{ - { - Quantity: 1, - Regions: []crossregionsubnets.CrossRegionOpt{ - { - Region: "ru-1", - }, - { - Region: "ru-3", - }, - }, - CIDR: "192.168.200.0/24", - }, - }, - } - newCrossRegionSubnets, _, err := crossregionsubnets.Create(ctx, resellClient, projectID, createOpts) - if err != nil { - log.Fatal(err) - } - for _, newCrossRegionSubnet := range newCrossRegionSubnets { - fmt.Printf("%v\n", newCrossRegionSubnet) - } - -Example of deleting a single cross-region subnet - - _, err = crossregionsubnets.Delete(ctx, resellClient, crossRegionSubnetID) - if err != nil { - log.Fatal(err) - } -*/ -package crossregionsubnets diff --git a/selvpcclient/resell/v2/crossregionsubnets/requests.go b/selvpcclient/resell/v2/crossregionsubnets/requests.go deleted file mode 100644 index e4fdfc9..0000000 --- a/selvpcclient/resell/v2/crossregionsubnets/requests.go +++ /dev/null @@ -1,110 +0,0 @@ -package crossregionsubnets - -import ( - "bytes" - "context" - "encoding/json" - "net/http" - "strings" - - "github.com/selectel/go-selvpcclient/selvpcclient" -) - -const resourceURL = "cross_region_subnets" - -// Get returns a single cross-region subnet by its id. -func Get(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*CrossRegionSubnet, *selvpcclient.ResponseResult, error) { - url := strings.Join([]string{client.Endpoint, resourceURL, id}, "/") - responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil) - if err != nil { - return nil, nil, err - } - if responseResult.Err != nil { - return nil, responseResult, responseResult.Err - } - - // Extract a cross-region subnet from the response body. - var result struct { - CrossRegionSubnet *CrossRegionSubnet `json:"cross_region_subnet"` - } - err = responseResult.ExtractResult(&result) - if err != nil { - return nil, responseResult, err - } - - return result.CrossRegionSubnet, responseResult, nil -} - -// List gets a list of cross-region subnets in the current domain. -func List(ctx context.Context, client *selvpcclient.ServiceClient, opts ListOpts) ([]*CrossRegionSubnet, *selvpcclient.ResponseResult, error) { - url := strings.Join([]string{client.Endpoint, resourceURL}, "/") - - queryParams, err := selvpcclient.BuildQueryParameters(opts) - if err != nil { - return nil, nil, err - } - if queryParams != "" { - url = strings.Join([]string{url, queryParams}, "?") - } - - responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil) - if err != nil { - return nil, nil, err - } - if responseResult.Err != nil { - return nil, responseResult, responseResult.Err - } - - // Extract cross-region subnets from the response body. - var result struct { - CrossRegionSubnets []*CrossRegionSubnet `json:"cross_region_subnets"` - } - err = responseResult.ExtractResult(&result) - if err != nil { - return nil, responseResult, err - } - - return result.CrossRegionSubnets, responseResult, nil -} - -// Create requests a creation of the cross-region subnets in the specified project. -func Create(ctx context.Context, client *selvpcclient.ServiceClient, projectID string, createOpts CrossRegionSubnetOpts) ([]*CrossRegionSubnet, *selvpcclient.ResponseResult, error) { - createCrossRegionSubnetsOpts := &createOpts - requestBody, err := json.Marshal(createCrossRegionSubnetsOpts) - if err != nil { - return nil, nil, err - } - - url := strings.Join([]string{client.Endpoint, resourceURL, "projects", projectID}, "/") - responseResult, err := client.DoRequest(ctx, http.MethodPost, url, bytes.NewReader(requestBody)) - if err != nil { - return nil, nil, err - } - if responseResult.Err != nil { - return nil, responseResult, responseResult.Err - } - - // Extract cross-region subnets from the response body. - var result struct { - CrossRegionSubnets []*CrossRegionSubnet `json:"cross_region_subnets"` - } - err = responseResult.ExtractResult(&result) - if err != nil { - return nil, responseResult, err - } - - return result.CrossRegionSubnets, responseResult, nil -} - -// Delete deletes a single cross-region subnet by its id. -func Delete(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*selvpcclient.ResponseResult, error) { - url := strings.Join([]string{client.Endpoint, resourceURL, id}, "/") - responseResult, err := client.DoRequest(ctx, http.MethodDelete, url, nil) - if err != nil { - return nil, err - } - if responseResult.Err != nil { - err = responseResult.Err - } - return responseResult, err -} diff --git a/selvpcclient/resell/v2/crossregionsubnets/requests_opts.go b/selvpcclient/resell/v2/crossregionsubnets/requests_opts.go deleted file mode 100644 index 8c47020..0000000 --- a/selvpcclient/resell/v2/crossregionsubnets/requests_opts.go +++ /dev/null @@ -1,30 +0,0 @@ -package crossregionsubnets - -// CrossRegionSubnetOpts represents options for the cross-region subnets Create request. -type CrossRegionSubnetOpts struct { - // CrossRegionSubnets represents options for all cross-region subnets. - CrossRegionSubnets []CrossRegionSubnetOpt `json:"cross_region_subnets"` -} - -// CrossRegionSubnetOpt represents options for the single cross-region subnet. -type CrossRegionSubnetOpt struct { - // Quantity represents how many subnets do we need to create. - Quantity int `json:"quantity"` - - // Regions represents region options for the cross-region subnet. - Regions []CrossRegionOpt `json:"regions"` - - // CIDR represents a subnet prefix in CIDR notation for the cross-region subnet. - CIDR string `json:"cidr"` -} - -// CrossRegionOpt represents region options for the cross-region subnet. -type CrossRegionOpt struct { - // Region represents region that cross-region subnet is associated to. - Region string `json:"region"` -} - -// ListOpts represents options for the List request. -type ListOpts struct { - Detailed bool `param:"detailed"` -} diff --git a/selvpcclient/resell/v2/crossregionsubnets/schemas.go b/selvpcclient/resell/v2/crossregionsubnets/schemas.go deleted file mode 100644 index d94e922..0000000 --- a/selvpcclient/resell/v2/crossregionsubnets/schemas.go +++ /dev/null @@ -1,30 +0,0 @@ -package crossregionsubnets - -import ( - "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/servers" - "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/subnets" -) - -// CrossRegionSubnet represents a single Resell cross-region subnet. -type CrossRegionSubnet struct { - // ID is a unique id of a cross-region subnet. - ID int `json:"id"` - - // CIDR is a cross-region subnet prefix in CIDR notation. - CIDR string `json:"cidr"` - - // VLANID represents id of the associated VLAN in the Networking service. - VLANID int `json:"vlan_id"` - - // Status shows if cross-region subnet is used. - Status string `json:"status"` - - // ProjectID represents an associated Identity service project. - ProjectID string `json:"project_id"` - - // Servers contains info about servers to which cross-region subnet is associated to. - Servers []servers.Server `json:"servers"` - - // Subnets contains standard subnets in every region that cross-region subnet is attached to. - Subnets []subnets.Subnet `json:"subnets"` -} diff --git a/selvpcclient/resell/v2/crossregionsubnets/testing/fixtures.go b/selvpcclient/resell/v2/crossregionsubnets/testing/fixtures.go deleted file mode 100644 index c88766a..0000000 --- a/selvpcclient/resell/v2/crossregionsubnets/testing/fixtures.go +++ /dev/null @@ -1,321 +0,0 @@ -package testing - -import ( - "time" - - "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/crossregionsubnets" - "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/servers" - "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/subnets" -) - -// TestGetCrossRegionSubnetResponseRaw represents a raw response from the Get request. -const TestGetCrossRegionSubnetResponseRaw = ` -{ - "cross_region_subnet": { - "id": 12, - "cidr": "192.168.200.0/24", - "vlan_id": 1003, - "status": "ACTIVE", - "project_id": "b63ab68796e34858befb8fa2a8b1e12a", - "servers": [ - { - "status": "ACTIVE", - "updated": "2019-01-04T08:09:43Z", - "id": "22170dcf-2e58-49b7-9115-951b84d366f6", - "name": "Node01" - }, - { - "status": "ACTIVE", - "updated": "2019-01-04T08:09:43Z", - "id": "df842202-fdcc-490e-b92a-6e252e5577c7", - "name": "Node02" - } - ], - "subnets": [ - { - "id": 10, - "vlan_id": 1003, - "cidr": "192.168.200.0/24", - "network_id": "78c1cbe1-c34d-4685-be2d-a877a1b1dec4", - "subnet_id": "7db1255f-2545-4b8a-9446-22608c0f6cb8", - "region": "ru-1", - "vtep_ip_address": "10.10.0.101" - }, - { - "id": 20, - "vlan_id": 1003, - "cidr": "192.168.200.0/24", - "network_id": "67f7ab15-9424-4b50-999a-1c4de12372ec", - "subnet_id": "66ee047b-c699-4d62-9b64-363d2d77f021", - "region": "ru-3", - "vtep_ip_address": "10.10.0.201" - } - ] - } -} -` - -var crossregionSubnetServerTimeStamp, _ = time.Parse(time.RFC3339, "2019-01-04T08:09:43Z") - -// TestGetCrossRegionSubnetResponse represents an unmarshalled TestGetCrossRegionSubnetResponseRaw. -var TestGetCrossRegionSubnetResponse = &crossregionsubnets.CrossRegionSubnet{ - ID: 12, - CIDR: "192.168.200.0/24", - VLANID: 1003, - Status: "ACTIVE", - ProjectID: "b63ab68796e34858befb8fa2a8b1e12a", - Servers: []servers.Server{ - { - ID: "22170dcf-2e58-49b7-9115-951b84d366f6", - Name: "Node01", - Status: "ACTIVE", - Updated: crossregionSubnetServerTimeStamp, - }, - { - ID: "df842202-fdcc-490e-b92a-6e252e5577c7", - Name: "Node02", - Status: "ACTIVE", - Updated: crossregionSubnetServerTimeStamp, - }, - }, - Subnets: []subnets.Subnet{ - { - ID: 10, - Region: "ru-1", - CIDR: "192.168.200.0/24", - NetworkID: "78c1cbe1-c34d-4685-be2d-a877a1b1dec4", - SubnetID: "7db1255f-2545-4b8a-9446-22608c0f6cb8", - VLANID: 1003, - VTEPIPAddress: "10.10.0.101", - }, - { - ID: 20, - Region: "ru-3", - CIDR: "192.168.200.0/24", - NetworkID: "67f7ab15-9424-4b50-999a-1c4de12372ec", - SubnetID: "66ee047b-c699-4d62-9b64-363d2d77f021", - VLANID: 1003, - VTEPIPAddress: "10.10.0.201", - }, - }, -} - -// TestListCrossRegionSubnetsResponseRaw represents a raw response from the List request. -const TestListCrossRegionSubnetsResponseRaw = ` -{ - "cross_region_subnets": [ - { - "id": 12, - "cidr": "192.168.200.0/24", - "vlan_id": 1003, - "status": "ACTIVE", - "project_id": "b63ab68796e34858befb8fa2a8b1e12a", - "servers": [ - { - "status": "ACTIVE", - "updated": "2019-01-04T08:09:43Z", - "id": "22170dcf-2e58-49b7-9115-951b84d366f6", - "name": "Node01" - }, - { - "status": "ACTIVE", - "updated": "2019-01-04T08:09:43Z", - "id": "df842202-fdcc-490e-b92a-6e252e5577c7", - "name": "Node02" - } - ], - "subnets": [ - { - "id": 10, - "vlan_id": 1003, - "cidr": "192.168.200.0/24", - "network_id": "78c1cbe1-c34d-4685-be2d-a877a1b1dec4", - "subnet_id": "7db1255f-2545-4b8a-9446-22608c0f6cb8", - "region": "ru-1", - "vtep_ip_address": "10.10.0.101" - }, - { - "id": 20, - "vlan_id": 1003, - "cidr": "192.168.200.0/24", - "network_id": "67f7ab15-9424-4b50-999a-1c4de12372ec", - "subnet_id": "66ee047b-c699-4d62-9b64-363d2d77f021", - "region": "ru-3", - "vtep_ip_address": "10.10.0.201" - } - ] - } - ] -} -` - -// TestListCrossRegionSubnetsResponse represents an unmarshalled TestListCrossRegionSubnetsResponseRaw -var TestListCrossRegionSubnetsResponse = []*crossregionsubnets.CrossRegionSubnet{ - { - ID: 12, - CIDR: "192.168.200.0/24", - VLANID: 1003, - Status: "ACTIVE", - ProjectID: "b63ab68796e34858befb8fa2a8b1e12a", - Servers: []servers.Server{ - { - ID: "22170dcf-2e58-49b7-9115-951b84d366f6", - Name: "Node01", - Status: "ACTIVE", - Updated: crossregionSubnetServerTimeStamp, - }, - { - ID: "df842202-fdcc-490e-b92a-6e252e5577c7", - Name: "Node02", - Status: "ACTIVE", - Updated: crossregionSubnetServerTimeStamp, - }, - }, - Subnets: []subnets.Subnet{ - { - ID: 10, - Region: "ru-1", - CIDR: "192.168.200.0/24", - NetworkID: "78c1cbe1-c34d-4685-be2d-a877a1b1dec4", - SubnetID: "7db1255f-2545-4b8a-9446-22608c0f6cb8", - VLANID: 1003, - VTEPIPAddress: "10.10.0.101", - }, - { - ID: 20, - Region: "ru-3", - CIDR: "192.168.200.0/24", - NetworkID: "67f7ab15-9424-4b50-999a-1c4de12372ec", - SubnetID: "66ee047b-c699-4d62-9b64-363d2d77f021", - VLANID: 1003, - VTEPIPAddress: "10.10.0.201", - }, - }, - }, -} - -// TestCreateCrossRegionSubnetsOptsRaw represents marshalled options for the Create request. -const TestCreateCrossRegionSubnetsOptsRaw = ` -{ - "cross_region_subnets": [ - { - "quantity": 1, - "regions": [ - { - "region": "ru-1" - }, - { - "region": "ru-3" - } - ], - "cidr": "192.168.200.0/24" - } - ] -} -` - -// TestCreateCrossRegionSubnetsOpts represents options for the Create request. -var TestCreateCrossRegionSubnetsOpts = crossregionsubnets.CrossRegionSubnetOpts{ - CrossRegionSubnets: []crossregionsubnets.CrossRegionSubnetOpt{ - { - Quantity: 1, - Regions: []crossregionsubnets.CrossRegionOpt{ - { - Region: "ru-1", - }, - { - Region: "ru-3", - }, - }, - CIDR: "192.168.200.0/24", - }, - }, -} - -// TestCreateCrossRegionSubnetsResponseRaw represents a raw response from the Create request. -const TestCreateCrossRegionSubnetsResponseRaw = ` -{ - "cross_region_subnets": [ - { - "id": 12, - "cidr": "192.168.200.0/24", - "vlan_id": 1003, - "status": "DOWN", - "project_id": "b63ab68796e34858befb8fa2a8b1e12a", - "subnets": [ - { - "id": 10, - "vlan_id": 1003, - "cidr": "192.168.200.0/24", - "network_id": "78c1cbe1-c34d-4685-be2d-a877a1b1dec4", - "subnet_id": "7db1255f-2545-4b8a-9446-22608c0f6cb8", - "region": "ru-1", - "vtep_ip_address": "10.10.0.101" - }, - { - "id": 20, - "vlan_id": 1003, - "cidr": "192.168.200.0/24", - "network_id": "67f7ab15-9424-4b50-999a-1c4de12372ec", - "subnet_id": "66ee047b-c699-4d62-9b64-363d2d77f021", - "region": "ru-3", - "vtep_ip_address": "10.10.0.201" - } - ] - } - ] -} -` - -// TestCreateCrossRegionSubnetsResponse represents an unmarshalled TestCreateCrossRegionSubnetsResponseRaw -var TestCreateCrossRegionSubnetsResponse = []*crossregionsubnets.CrossRegionSubnet{ - { - ID: 12, - CIDR: "192.168.200.0/24", - VLANID: 1003, - Status: "DOWN", - ProjectID: "b63ab68796e34858befb8fa2a8b1e12a", - Subnets: []subnets.Subnet{ - { - ID: 10, - Region: "ru-1", - CIDR: "192.168.200.0/24", - NetworkID: "78c1cbe1-c34d-4685-be2d-a877a1b1dec4", - SubnetID: "7db1255f-2545-4b8a-9446-22608c0f6cb8", - VLANID: 1003, - VTEPIPAddress: "10.10.0.101", - }, - { - ID: 20, - Region: "ru-3", - CIDR: "192.168.200.0/24", - NetworkID: "67f7ab15-9424-4b50-999a-1c4de12372ec", - SubnetID: "66ee047b-c699-4d62-9b64-363d2d77f021", - VLANID: 1003, - VTEPIPAddress: "10.10.0.201", - }, - }, - }, -} - -// TestManyCrossRegionSubnetsInvalidResponseRaw represents a raw invalid response with -// several cross-region subnets. -const TestManyCrossRegionSubnetsInvalidResponseRaw = ` -{ - "cross_region_subnets": [ - { - "id": "b63ab68796e34858befb8fa2a8b1e12a" - } - ] -} -` - -// TestSingleCrossRegionSubnetInvalidResponseRaw represents a raw invalid response with -// a single cross-region subnet. -const TestSingleCrossRegionSubnetInvalidResponseRaw = ` -{ - "cross_region_subnet": { - "id": "b63ab68796e34858befb8fa2a8b1e12a" - } -} -` diff --git a/selvpcclient/resell/v2/crossregionsubnets/testing/requests_test.go b/selvpcclient/resell/v2/crossregionsubnets/testing/requests_test.go deleted file mode 100644 index e3e2637..0000000 --- a/selvpcclient/resell/v2/crossregionsubnets/testing/requests_test.go +++ /dev/null @@ -1,416 +0,0 @@ -package testing - -import ( - "context" - "net/http" - "reflect" - "testing" - - "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/crossregionsubnets" - "github.com/selectel/go-selvpcclient/selvpcclient/testutils" -) - -func TestGetCrossRegionSubnet(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets/12", - RawResponse: TestGetCrossRegionSubnetResponseRaw, - Method: http.MethodGet, - Status: http.StatusOK, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - actual, _, err := crossregionsubnets.Get(ctx, testEnv.Client, "12") - if err != nil { - t.Fatal(err) - } - - expected := TestGetCrossRegionSubnetResponse - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if !reflect.DeepEqual(actual, expected) { - t.Fatalf("expected %#v, but got %#v", expected, actual) - } -} - -func TestGetCrossRegionSubnetHTTPError(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets/12", - RawResponse: TestGetCrossRegionSubnetResponseRaw, - Method: http.MethodGet, - Status: http.StatusBadGateway, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - crossRegionSubnet, httpResponse, err := crossregionsubnets.Get(ctx, testEnv.Client, "12") - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if crossRegionSubnet != nil { - t.Fatal("expected no cross-region subnet from the Get method") - } - if err == nil { - t.Fatal("expected error from the Get method") - } - if httpResponse.StatusCode != http.StatusBadGateway { - t.Fatalf("expected %d status in the HTTP response, but got %d", - http.StatusBadGateway, httpResponse.StatusCode) - } -} - -func TestGetCrossRegionSubnetTimeoutError(t *testing.T) { - testEnv := testutils.SetupTestEnv() - testEnv.Server.Close() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - - ctx := context.Background() - crossRegionSubnet, _, err := crossregionsubnets.Get(ctx, testEnv.Client, "12") - - if crossRegionSubnet != nil { - t.Fatal("expected no cross-region subnet from the Get method") - } - if err == nil { - t.Fatal("expected error from the Get method") - } -} - -func TestGetCrossRegionSubnetUnmarshalError(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets/12", - RawResponse: TestSingleCrossRegionSubnetInvalidResponseRaw, - Method: http.MethodGet, - Status: http.StatusOK, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - crossRegionSubnet, _, err := crossregionsubnets.Get(ctx, testEnv.Client, "12") - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if crossRegionSubnet != nil { - t.Fatal("expected no cross-region subnets from the Get method") - } - if err == nil { - t.Fatal("expected error from the Get method") - } -} - -func TestListCrossRegionSubnets(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets", - RawResponse: TestListCrossRegionSubnetsResponseRaw, - Method: http.MethodGet, - Status: http.StatusOK, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - actual, _, err := crossregionsubnets.List(ctx, testEnv.Client, crossregionsubnets.ListOpts{}) - if err != nil { - t.Fatal(err) - } - - expected := TestListCrossRegionSubnetsResponse - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if !reflect.DeepEqual(actual, expected) { - t.Fatalf("expected %#v, but got %#v", expected, actual) - } -} - -func TestListCrossRegionSubnetsHTTPError(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets", - RawResponse: TestListCrossRegionSubnetsResponseRaw, - Method: http.MethodGet, - Status: http.StatusBadGateway, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - allCrossRegionSubnets, httpResponse, err := crossregionsubnets.List(ctx, testEnv.Client, crossregionsubnets.ListOpts{}) - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if allCrossRegionSubnets != nil { - t.Fatal("expected no cross-region subnets from the List method") - } - if err == nil { - t.Fatal("expected error from the List method") - } - if httpResponse.StatusCode != http.StatusBadGateway { - t.Fatalf("expected %d status in the HTTP response, but got %d", - http.StatusBadGateway, httpResponse.StatusCode) - } -} - -func TestListCrossRegionSubnetsTimeoutError(t *testing.T) { - testEnv := testutils.SetupTestEnv() - testEnv.Server.Close() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - - ctx := context.Background() - allCrossRegionSubnets, _, err := crossregionsubnets.List(ctx, testEnv.Client, crossregionsubnets.ListOpts{}) - - if allCrossRegionSubnets != nil { - t.Fatal("expected no cross-region subnets from the List method") - } - if err == nil { - t.Fatal("expected error from the List method") - } -} - -func TestListCrossRegionSubnetsUnmarshalError(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets", - RawResponse: TestManyCrossRegionSubnetsInvalidResponseRaw, - Method: http.MethodGet, - Status: http.StatusOK, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - allCrossRegionSubnets, _, err := crossregionsubnets.List(ctx, testEnv.Client, crossregionsubnets.ListOpts{}) - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if allCrossRegionSubnets != nil { - t.Fatal("expected no cross-region subnets from the List method") - } - if err == nil { - t.Fatal("expected error from the List method") - } -} - -func TestCreateCrossRegionSubnets(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets/projects/b63ab68796e34858befb8fa2a8b1e12a", - RawResponse: TestCreateCrossRegionSubnetsResponseRaw, - RawRequest: TestCreateCrossRegionSubnetsOptsRaw, - Method: http.MethodPost, - Status: http.StatusOK, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - createOpts := TestCreateCrossRegionSubnetsOpts - actualResponse, _, err := crossregionsubnets.Create(ctx, testEnv.Client, "b63ab68796e34858befb8fa2a8b1e12a", createOpts) - if err != nil { - t.Fatal(err) - } - - expectedResponse := TestCreateCrossRegionSubnetsResponse - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if !reflect.DeepEqual(actualResponse, expectedResponse) { - t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse) - } -} - -func TestCreateCrossRegionSubnetsHTTPError(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets/projects/b63ab68796e34858befb8fa2a8b1e12a", - RawResponse: TestCreateCrossRegionSubnetsResponseRaw, - RawRequest: TestCreateCrossRegionSubnetsOptsRaw, - Method: http.MethodPost, - Status: http.StatusBadRequest, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - createOpts := TestCreateCrossRegionSubnetsOpts - crossregionSubnet, httpResponse, err := crossregionsubnets.Create(ctx, testEnv.Client, - "b63ab68796e34858befb8fa2a8b1e12a", createOpts) - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if crossregionSubnet != nil { - t.Fatal("expected no cross-region subnet from the Create method") - } - if err == nil { - t.Fatal("expected error from the Create method") - } - if httpResponse.StatusCode != http.StatusBadRequest { - t.Fatalf("expected %d status in the HTTP response, but got %d", - http.StatusBadRequest, httpResponse.StatusCode) - } -} - -func TestCreateCrossRegionSubnetsTimeoutError(t *testing.T) { - testEnv := testutils.SetupTestEnv() - testEnv.Server.Close() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - - ctx := context.Background() - createOpts := TestCreateCrossRegionSubnetsOpts - crossregionSubnet, _, err := crossregionsubnets.Create(ctx, testEnv.Client, "b63ab68796e34858befb8fa2a8b1e12a", createOpts) - - if crossregionSubnet != nil { - t.Fatal("expected no cross-region subnet from the Create method") - } - if err == nil { - t.Fatal("expected error from the Create method") - } -} - -func TestCreateCrossRegionSubnetsUnmarshalError(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets/projects/b63ab68796e34858befb8fa2a8b1e12a", - RawResponse: TestManyCrossRegionSubnetsInvalidResponseRaw, - RawRequest: TestCreateCrossRegionSubnetsOptsRaw, - Method: http.MethodPost, - Status: http.StatusOK, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - createOpts := TestCreateCrossRegionSubnetsOpts - crossregionSubnet, _, err := crossregionsubnets.Create(ctx, testEnv.Client, "b63ab68796e34858befb8fa2a8b1e12a", createOpts) - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if crossregionSubnet != nil { - t.Fatal("expected no cross-region subnet from the Create method") - } - if err == nil { - t.Fatal("expected error from the Create method") - } -} - -func TestDeleteCrossRegionSubnet(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets/12", - Method: http.MethodDelete, - Status: http.StatusOK, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - _, err := crossregionsubnets.Delete(ctx, testEnv.Client, "12") - if err != nil { - t.Fatal(err) - } - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } -} - -func TestDeleteCrossRegionSubnetHTTPError(t *testing.T) { - endpointCalled := false - - testEnv := testutils.SetupTestEnv() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{ - Mux: testEnv.Mux, - URL: "/resell/v2/cross_region_subnets/12", - Method: http.MethodDelete, - Status: http.StatusBadGateway, - CallFlag: &endpointCalled, - }) - - ctx := context.Background() - httpResponse, err := crossregionsubnets.Delete(ctx, testEnv.Client, "12") - - if !endpointCalled { - t.Fatal("endpoint wasn't called") - } - if err == nil { - t.Fatal("expected error from the Delete method") - } - if httpResponse.StatusCode != http.StatusBadGateway { - t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode) - } -} - -func TestDeleteCrossRegionSubnetTimeoutError(t *testing.T) { - testEnv := testutils.SetupTestEnv() - testEnv.Server.Close() - defer testEnv.TearDownTestEnv() - testEnv.NewTestResellV2Client() - - ctx := context.Background() - _, err := crossregionsubnets.Delete(ctx, testEnv.Client, "12") - - if err == nil { - t.Fatal("expected error from the Delete method") - } -}