github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/fwaas/routerinsertion/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/gophercloud/gophercloud"
     9  	fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
    10  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/firewalls"
    11  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/routerinsertion"
    12  	th "github.com/gophercloud/gophercloud/testhelper"
    13  )
    14  
    15  func TestCreate(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/v2.0/fw/firewalls", func(w http.ResponseWriter, r *http.Request) {
    20  		th.TestMethod(t, r, "POST")
    21  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    22  		th.TestHeader(t, r, "Content-Type", "application/json")
    23  		th.TestHeader(t, r, "Accept", "application/json")
    24  		th.TestJSONRequest(t, r, `
    25  {
    26      "firewall":{
    27          "name": "fw",
    28          "description": "OpenStack firewall",
    29          "admin_state_up": true,
    30          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
    31          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
    32          "router_ids": [
    33            "8a3a0d6a-34b5-4a92-b65d-6375a4c1e9e8"
    34          ]
    35      }
    36  }
    37        `)
    38  
    39  		w.Header().Add("Content-Type", "application/json")
    40  		w.WriteHeader(http.StatusCreated)
    41  
    42  		fmt.Fprintf(w, `
    43  {
    44      "firewall":{
    45          "status": "PENDING_CREATE",
    46          "name": "fw",
    47          "description": "OpenStack firewall",
    48          "admin_state_up": true,
    49          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
    50          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c"
    51      }
    52  }
    53      `)
    54  	})
    55  
    56  	firewallCreateOpts := firewalls.CreateOpts{
    57  		TenantID:     "b4eedccc6fb74fa8a7ad6b08382b852b",
    58  		Name:         "fw",
    59  		Description:  "OpenStack firewall",
    60  		AdminStateUp: gophercloud.Enabled,
    61  		PolicyID:     "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
    62  	}
    63  	createOpts := routerinsertion.CreateOptsExt{
    64  		CreateOptsBuilder: firewallCreateOpts,
    65  		RouterIDs:         []string{"8a3a0d6a-34b5-4a92-b65d-6375a4c1e9e8"},
    66  	}
    67  
    68  	_, err := firewalls.Create(fake.ServiceClient(), createOpts).Extract()
    69  	th.AssertNoErr(t, err)
    70  }
    71  
    72  func TestCreateWithNoRouters(t *testing.T) {
    73  	th.SetupHTTP()
    74  	defer th.TeardownHTTP()
    75  
    76  	th.Mux.HandleFunc("/v2.0/fw/firewalls", func(w http.ResponseWriter, r *http.Request) {
    77  		th.TestMethod(t, r, "POST")
    78  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    79  		th.TestHeader(t, r, "Content-Type", "application/json")
    80  		th.TestHeader(t, r, "Accept", "application/json")
    81  		th.TestJSONRequest(t, r, `
    82  {
    83      "firewall":{
    84          "name": "fw",
    85          "description": "OpenStack firewall",
    86          "admin_state_up": true,
    87          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
    88          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
    89          "router_ids": []
    90      }
    91  }
    92        `)
    93  
    94  		w.Header().Add("Content-Type", "application/json")
    95  		w.WriteHeader(http.StatusCreated)
    96  
    97  		fmt.Fprintf(w, `
    98  {
    99      "firewall":{
   100          "status": "PENDING_CREATE",
   101          "name": "fw",
   102          "description": "OpenStack firewall",
   103          "admin_state_up": true,
   104          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   105          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c"
   106      }
   107  }
   108      `)
   109  	})
   110  
   111  	firewallCreateOpts := firewalls.CreateOpts{
   112  		TenantID:     "b4eedccc6fb74fa8a7ad6b08382b852b",
   113  		Name:         "fw",
   114  		Description:  "OpenStack firewall",
   115  		AdminStateUp: gophercloud.Enabled,
   116  		PolicyID:     "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   117  	}
   118  	createOpts := routerinsertion.CreateOptsExt{
   119  		CreateOptsBuilder: firewallCreateOpts,
   120  		RouterIDs:         []string{},
   121  	}
   122  
   123  	_, err := firewalls.Create(fake.ServiceClient(), createOpts).Extract()
   124  	th.AssertNoErr(t, err)
   125  }
   126  
   127  func TestUpdate(t *testing.T) {
   128  	th.SetupHTTP()
   129  	defer th.TeardownHTTP()
   130  
   131  	th.Mux.HandleFunc("/v2.0/fw/firewalls/ea5b5315-64f6-4ea3-8e58-981cc37c6576", func(w http.ResponseWriter, r *http.Request) {
   132  		th.TestMethod(t, r, "PUT")
   133  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   134  		th.TestHeader(t, r, "Content-Type", "application/json")
   135  		th.TestHeader(t, r, "Accept", "application/json")
   136  		th.TestJSONRequest(t, r, `
   137  {
   138      "firewall":{
   139          "name": "fw",
   140          "description": "updated fw",
   141          "admin_state_up":false,
   142          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   143          "router_ids": [
   144            "8a3a0d6a-34b5-4a92-b65d-6375a4c1e9e8"
   145          ]
   146      }
   147  }
   148        `)
   149  
   150  		w.Header().Add("Content-Type", "application/json")
   151  		w.WriteHeader(http.StatusOK)
   152  
   153  		fmt.Fprintf(w, `
   154  {
   155      "firewall": {
   156          "status": "ACTIVE",
   157          "name": "fw",
   158          "admin_state_up": false,
   159          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   160          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   161          "id": "ea5b5315-64f6-4ea3-8e58-981cc37c6576",
   162          "description": "OpenStack firewall"
   163      }
   164  }
   165      `)
   166  	})
   167  
   168  	var name = "fw"
   169  	var description = "updated fw"
   170  	firewallUpdateOpts := firewalls.UpdateOpts{
   171  		Name:         &name,
   172  		Description:  &description,
   173  		AdminStateUp: gophercloud.Disabled,
   174  		PolicyID:     "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   175  	}
   176  	updateOpts := routerinsertion.UpdateOptsExt{
   177  		UpdateOptsBuilder: firewallUpdateOpts,
   178  		RouterIDs:         []string{"8a3a0d6a-34b5-4a92-b65d-6375a4c1e9e8"},
   179  	}
   180  
   181  	_, err := firewalls.Update(fake.ServiceClient(), "ea5b5315-64f6-4ea3-8e58-981cc37c6576", updateOpts).Extract()
   182  	th.AssertNoErr(t, err)
   183  }
   184  
   185  func TestUpdateWithNoRouters(t *testing.T) {
   186  	th.SetupHTTP()
   187  	defer th.TeardownHTTP()
   188  
   189  	th.Mux.HandleFunc("/v2.0/fw/firewalls/ea5b5315-64f6-4ea3-8e58-981cc37c6576", func(w http.ResponseWriter, r *http.Request) {
   190  		th.TestMethod(t, r, "PUT")
   191  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   192  		th.TestHeader(t, r, "Content-Type", "application/json")
   193  		th.TestHeader(t, r, "Accept", "application/json")
   194  		th.TestJSONRequest(t, r, `
   195  {
   196      "firewall":{
   197          "name": "fw",
   198          "description": "updated fw",
   199          "admin_state_up":false,
   200          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   201          "router_ids": []
   202      }
   203  }
   204        `)
   205  
   206  		w.Header().Add("Content-Type", "application/json")
   207  		w.WriteHeader(http.StatusOK)
   208  
   209  		fmt.Fprintf(w, `
   210  {
   211      "firewall": {
   212          "status": "ACTIVE",
   213          "name": "fw",
   214          "admin_state_up": false,
   215          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   216          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   217          "id": "ea5b5315-64f6-4ea3-8e58-981cc37c6576",
   218          "description": "OpenStack firewall"
   219      }
   220  }
   221      `)
   222  	})
   223  
   224  	var name = "fw"
   225  	var description = "updated fw"
   226  	firewallUpdateOpts := firewalls.UpdateOpts{
   227  		Name:         &name,
   228  		Description:  &description,
   229  		AdminStateUp: gophercloud.Disabled,
   230  		PolicyID:     "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   231  	}
   232  	updateOpts := routerinsertion.UpdateOptsExt{
   233  		UpdateOptsBuilder: firewallUpdateOpts,
   234  		RouterIDs:         []string{},
   235  	}
   236  
   237  	_, err := firewalls.Update(fake.ServiceClient(), "ea5b5315-64f6-4ea3-8e58-981cc37c6576", updateOpts).Extract()
   238  	th.AssertNoErr(t, err)
   239  }