github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/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/huaweicloud/golangsdk"
     9  	fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common"
    10  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/fwaas/firewalls"
    11  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/fwaas/routerinsertion"
    12  	th "github.com/huaweicloud/golangsdk/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: golangsdk.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: golangsdk.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  	firewallUpdateOpts := firewalls.UpdateOpts{
   169  		Name:         "fw",
   170  		Description:  "updated fw",
   171  		AdminStateUp: golangsdk.Disabled,
   172  		PolicyID:     "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   173  	}
   174  	updateOpts := routerinsertion.UpdateOptsExt{
   175  		UpdateOptsBuilder: firewallUpdateOpts,
   176  		RouterIDs:         []string{"8a3a0d6a-34b5-4a92-b65d-6375a4c1e9e8"},
   177  	}
   178  
   179  	_, err := firewalls.Update(fake.ServiceClient(), "ea5b5315-64f6-4ea3-8e58-981cc37c6576", updateOpts).Extract()
   180  	th.AssertNoErr(t, err)
   181  }
   182  
   183  func TestUpdateWithNoRouters(t *testing.T) {
   184  	th.SetupHTTP()
   185  	defer th.TeardownHTTP()
   186  
   187  	th.Mux.HandleFunc("/v2.0/fw/firewalls/ea5b5315-64f6-4ea3-8e58-981cc37c6576", func(w http.ResponseWriter, r *http.Request) {
   188  		th.TestMethod(t, r, "PUT")
   189  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   190  		th.TestHeader(t, r, "Content-Type", "application/json")
   191  		th.TestHeader(t, r, "Accept", "application/json")
   192  		th.TestJSONRequest(t, r, `
   193  {
   194      "firewall":{
   195          "name": "fw",
   196          "description": "updated fw",
   197          "admin_state_up":false,
   198          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   199          "router_ids": []
   200      }
   201  }
   202        `)
   203  
   204  		w.Header().Add("Content-Type", "application/json")
   205  		w.WriteHeader(http.StatusOK)
   206  
   207  		fmt.Fprintf(w, `
   208  {
   209      "firewall": {
   210          "status": "ACTIVE",
   211          "name": "fw",
   212          "admin_state_up": false,
   213          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   214          "firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   215          "id": "ea5b5315-64f6-4ea3-8e58-981cc37c6576",
   216          "description": "OpenStack firewall"
   217      }
   218  }
   219      `)
   220  	})
   221  
   222  	firewallUpdateOpts := firewalls.UpdateOpts{
   223  		Name:         "fw",
   224  		Description:  "updated fw",
   225  		AdminStateUp: golangsdk.Disabled,
   226  		PolicyID:     "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
   227  	}
   228  	updateOpts := routerinsertion.UpdateOptsExt{
   229  		UpdateOptsBuilder: firewallUpdateOpts,
   230  		RouterIDs:         []string{},
   231  	}
   232  
   233  	_, err := firewalls.Update(fake.ServiceClient(), "ea5b5315-64f6-4ea3-8e58-981cc37c6576", updateOpts).Extract()
   234  	th.AssertNoErr(t, err)
   235  }