github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/vpnaas/siteconnections/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/vnpaycloud-console/gophercloud/v2"
    10  	fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/common"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/vpnaas/siteconnections"
    12  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    13  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    14  )
    15  
    16  func TestCreate(t *testing.T) {
    17  	th.SetupHTTP()
    18  	defer th.TeardownHTTP()
    19  
    20  	th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections", func(w http.ResponseWriter, r *http.Request) {
    21  		th.TestMethod(t, r, "POST")
    22  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    23  		th.TestHeader(t, r, "Content-Type", "application/json")
    24  		th.TestHeader(t, r, "Accept", "application/json")
    25  		th.TestJSONRequest(t, r, `
    26  {
    27      
    28      "ipsec_site_connection": {
    29          "psk": "secret",
    30          "initiator": "bi-directional",
    31          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
    32          "admin_state_up": true,
    33          "mtu": 1500,
    34          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
    35          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
    36          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
    37          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
    38          "peer_address": "172.24.4.233",
    39          "peer_id": "172.24.4.233",
    40          "name": "vpnconnection1"
    41      
    42  }
    43  }      `)
    44  
    45  		w.Header().Add("Content-Type", "application/json")
    46  		w.WriteHeader(http.StatusCreated)
    47  
    48  		fmt.Fprint(w, `
    49  {
    50      "ipsec_site_connection": {
    51          "status": "PENDING_CREATE",
    52          "psk": "secret",
    53          "initiator": "bi-directional",
    54          "name": "vpnconnection1",
    55          "admin_state_up": true,
    56          "project_id": "10039663455a446d8ba2cbb058b0f578",
    57          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
    58          "auth_mode": "psk",
    59          "peer_cidrs": [],
    60          "mtu": 1500,
    61          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
    62          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
    63          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
    64          "dpd": {
    65              "action": "hold",
    66              "interval": 30,
    67              "timeout": 120
    68          },
    69          "route_mode": "static",
    70          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
    71          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
    72          "peer_address": "172.24.4.233",
    73          "peer_id": "172.24.4.233",
    74          "id": "851f280f-5639-4ea3-81aa-e298525ab74b",
    75          "description": ""
    76      }
    77  }
    78      `)
    79  	})
    80  
    81  	options := siteconnections.CreateOpts{
    82  		Name:           "vpnconnection1",
    83  		AdminStateUp:   gophercloud.Enabled,
    84  		PSK:            "secret",
    85  		Initiator:      siteconnections.InitiatorBiDirectional,
    86  		IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
    87  		MTU:            1500,
    88  		PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
    89  		IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
    90  		VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
    91  		LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
    92  		PeerAddress:    "172.24.4.233",
    93  		PeerID:         "172.24.4.233",
    94  	}
    95  	actual, err := siteconnections.Create(context.TODO(), fake.ServiceClient(), options).Extract()
    96  	th.AssertNoErr(t, err)
    97  	expectedDPD := siteconnections.DPD{
    98  		Action:   "hold",
    99  		Interval: 30,
   100  		Timeout:  120,
   101  	}
   102  	expected := siteconnections.Connection{
   103  		TenantID:       "10039663455a446d8ba2cbb058b0f578",
   104  		Name:           "vpnconnection1",
   105  		AdminStateUp:   true,
   106  		PSK:            "secret",
   107  		Initiator:      "bi-directional",
   108  		IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   109  		MTU:            1500,
   110  		PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   111  		IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   112  		VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   113  		LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
   114  		PeerAddress:    "172.24.4.233",
   115  		PeerID:         "172.24.4.233",
   116  		Status:         "PENDING_CREATE",
   117  		ProjectID:      "10039663455a446d8ba2cbb058b0f578",
   118  		AuthMode:       "psk",
   119  		PeerCIDRs:      []string{},
   120  		DPD:            expectedDPD,
   121  		RouteMode:      "static",
   122  		ID:             "851f280f-5639-4ea3-81aa-e298525ab74b",
   123  		Description:    "",
   124  	}
   125  	th.AssertDeepEquals(t, expected, *actual)
   126  }
   127  
   128  func TestDelete(t *testing.T) {
   129  	th.SetupHTTP()
   130  	defer th.TeardownHTTP()
   131  
   132  	th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   133  		th.TestMethod(t, r, "DELETE")
   134  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   135  		w.WriteHeader(http.StatusNoContent)
   136  	})
   137  
   138  	res := siteconnections.Delete(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828")
   139  	th.AssertNoErr(t, res.Err)
   140  }
   141  
   142  func TestGet(t *testing.T) {
   143  	th.SetupHTTP()
   144  	defer th.TeardownHTTP()
   145  
   146  	th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   147  		th.TestMethod(t, r, "GET")
   148  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   149  
   150  		w.Header().Add("Content-Type", "application/json")
   151  		w.WriteHeader(http.StatusOK)
   152  
   153  		fmt.Fprint(w, `
   154  {
   155      "ipsec_site_connection": {
   156          "status": "PENDING_CREATE",
   157          "psk": "secret",
   158          "initiator": "bi-directional",
   159          "name": "vpnconnection1",
   160          "admin_state_up": true,
   161          "project_id": "10039663455a446d8ba2cbb058b0f578",
   162          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
   163          "auth_mode": "psk",
   164          "peer_cidrs": [],
   165          "mtu": 1500,
   166          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   167          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   168          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   169          "dpd": {
   170              "action": "hold",
   171              "interval": 30,
   172              "timeout": 120
   173          },
   174          "route_mode": "static",
   175          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   176          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
   177          "peer_address": "172.24.4.233",
   178          "peer_id": "172.24.4.233",
   179          "id": "851f280f-5639-4ea3-81aa-e298525ab74b",
   180          "description": ""
   181      }
   182  }
   183          `)
   184  	})
   185  
   186  	actual, err := siteconnections.Get(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828").Extract()
   187  	th.AssertNoErr(t, err)
   188  	expectedDPD := siteconnections.DPD{
   189  		Action:   "hold",
   190  		Interval: 30,
   191  		Timeout:  120,
   192  	}
   193  	expected := siteconnections.Connection{
   194  		TenantID:       "10039663455a446d8ba2cbb058b0f578",
   195  		Name:           "vpnconnection1",
   196  		AdminStateUp:   true,
   197  		PSK:            "secret",
   198  		Initiator:      "bi-directional",
   199  		IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   200  		MTU:            1500,
   201  		PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   202  		IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   203  		VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   204  		LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
   205  		PeerAddress:    "172.24.4.233",
   206  		PeerID:         "172.24.4.233",
   207  		Status:         "PENDING_CREATE",
   208  		ProjectID:      "10039663455a446d8ba2cbb058b0f578",
   209  		AuthMode:       "psk",
   210  		PeerCIDRs:      []string{},
   211  		DPD:            expectedDPD,
   212  		RouteMode:      "static",
   213  		ID:             "851f280f-5639-4ea3-81aa-e298525ab74b",
   214  		Description:    "",
   215  	}
   216  	th.AssertDeepEquals(t, expected, *actual)
   217  }
   218  
   219  func TestList(t *testing.T) {
   220  	th.SetupHTTP()
   221  	defer th.TeardownHTTP()
   222  
   223  	th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections", func(w http.ResponseWriter, r *http.Request) {
   224  		th.TestMethod(t, r, "GET")
   225  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   226  
   227  		w.Header().Add("Content-Type", "application/json")
   228  		w.WriteHeader(http.StatusOK)
   229  
   230  		fmt.Fprint(w, `
   231  {
   232      "ipsec_site_connections":[
   233  	{
   234          "status": "PENDING_CREATE",
   235          "psk": "secret",
   236          "initiator": "bi-directional",
   237          "name": "vpnconnection1",
   238          "admin_state_up": true,
   239          "project_id": "10039663455a446d8ba2cbb058b0f578",
   240          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
   241          "auth_mode": "psk",
   242          "peer_cidrs": [],
   243          "mtu": 1500,
   244          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   245          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   246          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   247          "dpd": {
   248              "action": "hold",
   249              "interval": 30,
   250              "timeout": 120
   251          },
   252          "route_mode": "static",
   253          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   254          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
   255          "peer_address": "172.24.4.233",
   256          "peer_id": "172.24.4.233",
   257          "id": "851f280f-5639-4ea3-81aa-e298525ab74b",
   258          "description": ""
   259      }]
   260  }
   261        `)
   262  	})
   263  
   264  	count := 0
   265  
   266  	err := siteconnections.List(fake.ServiceClient(), siteconnections.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   267  		count++
   268  		actual, err := siteconnections.ExtractConnections(page)
   269  		if err != nil {
   270  			t.Errorf("Failed to extract members: %v", err)
   271  			return false, err
   272  		}
   273  
   274  		expectedDPD := siteconnections.DPD{
   275  			Action:   "hold",
   276  			Interval: 30,
   277  			Timeout:  120,
   278  		}
   279  		expected := []siteconnections.Connection{
   280  			{
   281  				TenantID:       "10039663455a446d8ba2cbb058b0f578",
   282  				Name:           "vpnconnection1",
   283  				AdminStateUp:   true,
   284  				PSK:            "secret",
   285  				Initiator:      "bi-directional",
   286  				IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   287  				MTU:            1500,
   288  				PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   289  				IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   290  				VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   291  				LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
   292  				PeerAddress:    "172.24.4.233",
   293  				PeerID:         "172.24.4.233",
   294  				Status:         "PENDING_CREATE",
   295  				ProjectID:      "10039663455a446d8ba2cbb058b0f578",
   296  				AuthMode:       "psk",
   297  				PeerCIDRs:      []string{},
   298  				DPD:            expectedDPD,
   299  				RouteMode:      "static",
   300  				ID:             "851f280f-5639-4ea3-81aa-e298525ab74b",
   301  				Description:    "",
   302  			},
   303  		}
   304  
   305  		th.CheckDeepEquals(t, expected, actual)
   306  
   307  		return true, nil
   308  	})
   309  	th.AssertNoErr(t, err)
   310  
   311  	if count != 1 {
   312  		t.Errorf("Expected 1 page, got %d", count)
   313  	}
   314  }
   315  
   316  func TestUpdate(t *testing.T) {
   317  	th.SetupHTTP()
   318  	defer th.TeardownHTTP()
   319  
   320  	th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   321  		th.TestMethod(t, r, "PUT")
   322  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   323  		th.TestHeader(t, r, "Content-Type", "application/json")
   324  		th.TestHeader(t, r, "Accept", "application/json")
   325  		th.TestJSONRequest(t, r, `
   326  	{
   327  		  "ipsec_site_connection": {
   328  				"psk": "updatedsecret",
   329  				"initiator": "response-only",
   330  				"name": "updatedconnection",
   331  				"description": "updateddescription"
   332  		}
   333  	}
   334  	`)
   335  
   336  		w.Header().Add("Content-Type", "application/json")
   337  		w.WriteHeader(http.StatusOK)
   338  
   339  		fmt.Fprint(w, `
   340  
   341  	{
   342      "ipsec_site_connection": {
   343          "status": "ACTIVE",
   344          "psk": "updatedsecret",
   345          "initiator": "response-only",
   346          "name": "updatedconnection",
   347          "admin_state_up": true,
   348          "project_id": "10039663455a446d8ba2cbb058b0f578",
   349          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
   350          "auth_mode": "psk",
   351          "peer_cidrs": [],
   352          "mtu": 1500,
   353          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   354          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   355          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   356          "dpd": {
   357              "action": "hold",
   358              "interval": 30,
   359              "timeout": 120
   360          },
   361          "route_mode": "static",
   362          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   363          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
   364          "peer_address": "172.24.4.233",
   365          "peer_id": "172.24.4.233",
   366          "id": "851f280f-5639-4ea3-81aa-e298525ab74b",
   367          "description": "updateddescription"
   368      }
   369  }
   370  }
   371  `)
   372  	})
   373  	updatedName := "updatedconnection"
   374  	updatedDescription := "updateddescription"
   375  	options := siteconnections.UpdateOpts{
   376  		Name:        &updatedName,
   377  		Description: &updatedDescription,
   378  		Initiator:   siteconnections.InitiatorResponseOnly,
   379  		PSK:         "updatedsecret",
   380  	}
   381  
   382  	actual, err := siteconnections.Update(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828", options).Extract()
   383  	th.AssertNoErr(t, err)
   384  
   385  	expectedDPD := siteconnections.DPD{
   386  		Action:   "hold",
   387  		Interval: 30,
   388  		Timeout:  120,
   389  	}
   390  
   391  	expected := siteconnections.Connection{
   392  		TenantID:       "10039663455a446d8ba2cbb058b0f578",
   393  		Name:           "updatedconnection",
   394  		AdminStateUp:   true,
   395  		PSK:            "updatedsecret",
   396  		Initiator:      "response-only",
   397  		IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   398  		MTU:            1500,
   399  		PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   400  		IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   401  		VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   402  		LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
   403  		PeerAddress:    "172.24.4.233",
   404  		PeerID:         "172.24.4.233",
   405  		Status:         "ACTIVE",
   406  		ProjectID:      "10039663455a446d8ba2cbb058b0f578",
   407  		AuthMode:       "psk",
   408  		PeerCIDRs:      []string{},
   409  		DPD:            expectedDPD,
   410  		RouteMode:      "static",
   411  		ID:             "851f280f-5639-4ea3-81aa-e298525ab74b",
   412  		Description:    "updateddescription",
   413  	}
   414  	th.AssertDeepEquals(t, expected, *actual)
   415  }