github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/fwaas_v2/policies/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_v2/policies"
    11  	"github.com/gophercloud/gophercloud/pagination"
    12  	th "github.com/gophercloud/gophercloud/testhelper"
    13  )
    14  
    15  func TestList(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies", func(w http.ResponseWriter, r *http.Request) {
    20  		th.TestMethod(t, r, "GET")
    21  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    22  
    23  		w.Header().Add("Content-Type", "application/json")
    24  		w.WriteHeader(http.StatusOK)
    25  
    26  		fmt.Fprintf(w, `
    27  {
    28      "firewall_policies": [
    29          {
    30              "name": "policy1",
    31              "firewall_rules": [
    32                  "75452b36-268e-4e75-aaf4-f0e7ed50bc97",
    33                  "c9e77ca0-1bc8-497d-904d-948107873dc6"
    34              ],
    35              "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
    36  			"project_id": "9145d91459d248b1b02fdaca97c6a75d",
    37              "audited": true,
    38  			"shared": false,
    39              "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
    40              "description": "Firewall policy 1"
    41          },
    42          {
    43              "name": "policy2",
    44              "firewall_rules": [
    45                  "03d2a6ad-633f-431a-8463-4370d06a22c8"
    46              ],
    47              "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
    48  			"project_id": "9145d91459d248b1b02fdaca97c6a75d",
    49              "audited": false,
    50  			"shared": true,
    51              "id": "c854fab5-bdaf-4a86-9359-78de93e5df01",
    52              "description": "Firewall policy 2"
    53          }
    54      ]
    55  }
    56          `)
    57  	})
    58  
    59  	count := 0
    60  
    61  	policies.List(fake.ServiceClient(), policies.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    62  		count++
    63  		actual, err := policies.ExtractPolicies(page)
    64  		if err != nil {
    65  			t.Errorf("Failed to extract members: %v", err)
    66  			return false, err
    67  		}
    68  
    69  		expected := []policies.Policy{
    70  			{
    71  				Name: "policy1",
    72  				Rules: []string{
    73  					"75452b36-268e-4e75-aaf4-f0e7ed50bc97",
    74  					"c9e77ca0-1bc8-497d-904d-948107873dc6",
    75  				},
    76  				TenantID:    "9145d91459d248b1b02fdaca97c6a75d",
    77  				ProjectID:   "9145d91459d248b1b02fdaca97c6a75d",
    78  				Audited:     true,
    79  				Shared:      false,
    80  				ID:          "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
    81  				Description: "Firewall policy 1",
    82  			},
    83  			{
    84  				Name: "policy2",
    85  				Rules: []string{
    86  					"03d2a6ad-633f-431a-8463-4370d06a22c8",
    87  				},
    88  				TenantID:    "9145d91459d248b1b02fdaca97c6a75d",
    89  				ProjectID:   "9145d91459d248b1b02fdaca97c6a75d",
    90  				Audited:     false,
    91  				Shared:      true,
    92  				ID:          "c854fab5-bdaf-4a86-9359-78de93e5df01",
    93  				Description: "Firewall policy 2",
    94  			},
    95  		}
    96  
    97  		th.CheckDeepEquals(t, expected, actual)
    98  
    99  		return true, nil
   100  	})
   101  
   102  	if count != 1 {
   103  		t.Errorf("Expected 1 page, got %d", count)
   104  	}
   105  }
   106  
   107  func TestCreate(t *testing.T) {
   108  	th.SetupHTTP()
   109  	defer th.TeardownHTTP()
   110  
   111  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies", func(w http.ResponseWriter, r *http.Request) {
   112  		th.TestMethod(t, r, "POST")
   113  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   114  		th.TestHeader(t, r, "Content-Type", "application/json")
   115  		th.TestHeader(t, r, "Accept", "application/json")
   116  		th.TestJSONRequest(t, r, `
   117  {
   118      "firewall_policy":{
   119          "name": "policy",
   120          "firewall_rules": [
   121              "98a58c87-76be-ae7c-a74e-b77fffb88d95",
   122              "11a58c87-76be-ae7c-a74e-b77fffb88a32"
   123          ],
   124          "description": "Firewall policy",
   125  		"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   126  		"project_id": "9145d91459d248b1b02fdaca97c6a75d",
   127  		"audited": true,
   128  		"shared": false
   129      }
   130  }
   131        `)
   132  
   133  		w.Header().Add("Content-Type", "application/json")
   134  		w.WriteHeader(http.StatusCreated)
   135  
   136  		fmt.Fprintf(w, `
   137  {
   138      "firewall_policy":{
   139          "name": "policy",
   140          "firewall_rules": [
   141              "98a58c87-76be-ae7c-a74e-b77fffb88d95",
   142              "11a58c87-76be-ae7c-a74e-b77fffb88a32"
   143          ],
   144          "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   145  		"project_id": "9145d91459d248b1b02fdaca97c6a75d",
   146          "audited": false,
   147          "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
   148          "description": "Firewall policy"
   149      }
   150  }
   151          `)
   152  	})
   153  
   154  	options := policies.CreateOpts{
   155  		TenantID:    "9145d91459d248b1b02fdaca97c6a75d",
   156  		ProjectID:   "9145d91459d248b1b02fdaca97c6a75d",
   157  		Name:        "policy",
   158  		Description: "Firewall policy",
   159  		Shared:      gophercloud.Disabled,
   160  		Audited:     gophercloud.Enabled,
   161  		FirewallRules: []string{
   162  			"98a58c87-76be-ae7c-a74e-b77fffb88d95",
   163  			"11a58c87-76be-ae7c-a74e-b77fffb88a32",
   164  		},
   165  	}
   166  
   167  	_, err := policies.Create(fake.ServiceClient(), options).Extract()
   168  	th.AssertNoErr(t, err)
   169  }
   170  
   171  func TestInsertRule(t *testing.T) {
   172  	th.SetupHTTP()
   173  	defer th.TeardownHTTP()
   174  
   175  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/e3c78ab6-e827-4297-8d68-739063865a8b/insert_rule", func(w http.ResponseWriter, r *http.Request) {
   176  		th.TestMethod(t, r, "PUT")
   177  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   178  		th.TestHeader(t, r, "Content-Type", "application/json")
   179  		th.TestHeader(t, r, "Accept", "application/json")
   180  		th.TestJSONRequest(t, r, `
   181  {
   182      "firewall_rule_id": "7d305689-6cb1-4e75-9f4d-517b9ba792b5",
   183      "insert_before": "3062ed90-1fb0-4c25-af3d-318dff2143ae"
   184  }
   185          `)
   186  
   187  		w.Header().Add("Content-Type", "application/json")
   188  		w.WriteHeader(http.StatusOK)
   189  
   190  		fmt.Fprintf(w, `
   191  {
   192      "audited": false,
   193      "description": "TESTACC-DESC-8P12aLfW",
   194      "firewall_rules": [
   195          "7d305689-6cb1-4e75-9f4d-517b9ba792b5",
   196          "3062ed90-1fb0-4c25-af3d-318dff2143ae"
   197      ],
   198      "id": "e3c78ab6-e827-4297-8d68-739063865a8b",
   199      "name": "TESTACC-2LnMayeG",
   200      "project_id": "9f98fc0e5f944cd1b51798b668dc8778",
   201      "shared": false,
   202      "tenant_id": "9f98fc0e5f944cd1b51798b668dc8778"
   203  }
   204      `)
   205  	})
   206  
   207  	options := policies.InsertRuleOpts{
   208  		ID:           "7d305689-6cb1-4e75-9f4d-517b9ba792b5",
   209  		InsertBefore: "3062ed90-1fb0-4c25-af3d-318dff2143ae",
   210  	}
   211  
   212  	policy, err := policies.InsertRule(fake.ServiceClient(), "e3c78ab6-e827-4297-8d68-739063865a8b", options).Extract()
   213  	th.AssertNoErr(t, err)
   214  	th.AssertEquals(t, "TESTACC-2LnMayeG", policy.Name)
   215  	th.AssertEquals(t, 2, len(policy.Rules))
   216  	th.AssertEquals(t, "7d305689-6cb1-4e75-9f4d-517b9ba792b5", policy.Rules[0])
   217  	th.AssertEquals(t, "3062ed90-1fb0-4c25-af3d-318dff2143ae", policy.Rules[1])
   218  	th.AssertEquals(t, "e3c78ab6-e827-4297-8d68-739063865a8b", policy.ID)
   219  	th.AssertEquals(t, "TESTACC-DESC-8P12aLfW", policy.Description)
   220  	th.AssertEquals(t, "9f98fc0e5f944cd1b51798b668dc8778", policy.TenantID)
   221  	th.AssertEquals(t, "9f98fc0e5f944cd1b51798b668dc8778", policy.ProjectID)
   222  }
   223  
   224  func TestInsertRuleWithInvalidParameters(t *testing.T) {
   225  	th.SetupHTTP()
   226  	defer th.TeardownHTTP()
   227  
   228  	//invalid opts, its not allowed to specify InsertBefore and InsertAfter together
   229  	options := policies.InsertRuleOpts{
   230  		ID:           "unknown",
   231  		InsertBefore: "1",
   232  		InsertAfter:  "2",
   233  	}
   234  
   235  	_, err := policies.InsertRule(fake.ServiceClient(), "0", options).Extract()
   236  
   237  	// expect to fail with an gophercloud error
   238  	th.AssertErr(t, err)
   239  	th.AssertEquals(t, "Exactly one of InsertBefore and InsertAfter must be provided", err.Error())
   240  }
   241  
   242  func TestGet(t *testing.T) {
   243  	th.SetupHTTP()
   244  	defer th.TeardownHTTP()
   245  
   246  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/f2b08c1e-aa81-4668-8ae1-1401bcb0576c", func(w http.ResponseWriter, r *http.Request) {
   247  		th.TestMethod(t, r, "GET")
   248  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   249  
   250  		w.Header().Add("Content-Type", "application/json")
   251  		w.WriteHeader(http.StatusOK)
   252  
   253  		fmt.Fprintf(w, `
   254  {
   255      "firewall_policy":{
   256          "name": "www",
   257          "firewall_rules": [
   258              "75452b36-268e-4e75-aaf4-f0e7ed50bc97",
   259              "c9e77ca0-1bc8-497d-904d-948107873dc6",
   260              "03d2a6ad-633f-431a-8463-4370d06a22c8"
   261          ],
   262          "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   263  		"project_id": "9145d91459d248b1b02fdaca97c6a75d",
   264          "audited": false,
   265          "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
   266          "description": "Firewall policy web"
   267      }
   268  }
   269          `)
   270  	})
   271  
   272  	policy, err := policies.Get(fake.ServiceClient(), "f2b08c1e-aa81-4668-8ae1-1401bcb0576c").Extract()
   273  	th.AssertNoErr(t, err)
   274  
   275  	th.AssertEquals(t, "www", policy.Name)
   276  	th.AssertEquals(t, "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", policy.ID)
   277  	th.AssertEquals(t, "Firewall policy web", policy.Description)
   278  	th.AssertEquals(t, 3, len(policy.Rules))
   279  	th.AssertEquals(t, "75452b36-268e-4e75-aaf4-f0e7ed50bc97", policy.Rules[0])
   280  	th.AssertEquals(t, "c9e77ca0-1bc8-497d-904d-948107873dc6", policy.Rules[1])
   281  	th.AssertEquals(t, "03d2a6ad-633f-431a-8463-4370d06a22c8", policy.Rules[2])
   282  	th.AssertEquals(t, "9145d91459d248b1b02fdaca97c6a75d", policy.TenantID)
   283  	th.AssertEquals(t, "9145d91459d248b1b02fdaca97c6a75d", policy.ProjectID)
   284  }
   285  
   286  func TestUpdate(t *testing.T) {
   287  	th.SetupHTTP()
   288  	defer th.TeardownHTTP()
   289  
   290  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/f2b08c1e-aa81-4668-8ae1-1401bcb0576c", func(w http.ResponseWriter, r *http.Request) {
   291  		th.TestMethod(t, r, "PUT")
   292  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   293  		th.TestHeader(t, r, "Content-Type", "application/json")
   294  		th.TestHeader(t, r, "Accept", "application/json")
   295  		th.TestJSONRequest(t, r, `
   296  {
   297      "firewall_policy":{
   298          "name": "policy",
   299          "firewall_rules": [
   300              "98a58c87-76be-ae7c-a74e-b77fffb88d95",
   301              "11a58c87-76be-ae7c-a74e-b77fffb88a32"
   302          ],
   303          "description": "Firewall policy"
   304      }
   305  }
   306        `)
   307  
   308  		w.Header().Add("Content-Type", "application/json")
   309  		w.WriteHeader(http.StatusOK)
   310  
   311  		fmt.Fprintf(w, `
   312  {
   313      "firewall_policy":{
   314          "name": "policy",
   315          "firewall_rules": [
   316              "75452b36-268e-4e75-aaf4-f0e7ed50bc97",
   317              "c9e77ca0-1bc8-497d-904d-948107873dc6",
   318              "03d2a6ad-633f-431a-8463-4370d06a22c8"
   319          ],
   320          "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   321          "project_id": "9145d91459d248b1b02fdaca97c6a75d",
   322          "audited": false,
   323          "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
   324          "description": "Firewall policy"
   325      }
   326  }
   327      `)
   328  	})
   329  
   330  	name := "policy"
   331  	description := "Firewall policy"
   332  
   333  	options := policies.UpdateOpts{
   334  		Name:        &name,
   335  		Description: &description,
   336  		FirewallRules: &[]string{
   337  			"98a58c87-76be-ae7c-a74e-b77fffb88d95",
   338  			"11a58c87-76be-ae7c-a74e-b77fffb88a32",
   339  		},
   340  	}
   341  
   342  	_, err := policies.Update(fake.ServiceClient(), "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", options).Extract()
   343  	th.AssertNoErr(t, err)
   344  }
   345  
   346  func TestDelete(t *testing.T) {
   347  	th.SetupHTTP()
   348  	defer th.TeardownHTTP()
   349  
   350  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/4ec89077-d057-4a2b-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
   351  		th.TestMethod(t, r, "DELETE")
   352  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   353  		w.WriteHeader(http.StatusNoContent)
   354  	})
   355  
   356  	res := policies.Delete(fake.ServiceClient(), "4ec89077-d057-4a2b-911f-60a3b47ee304")
   357  	th.AssertNoErr(t, res.Err)
   358  }
   359  
   360  func TestRemoveRule(t *testing.T) {
   361  	th.SetupHTTP()
   362  	defer th.TeardownHTTP()
   363  
   364  	th.Mux.HandleFunc("/v2.0/fwaas/firewall_policies/9fed8075-06ee-463f-83a6-d4118791b02f/remove_rule", func(w http.ResponseWriter, r *http.Request) {
   365  		th.TestMethod(t, r, "PUT")
   366  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   367  		th.TestHeader(t, r, "Content-Type", "application/json")
   368  		th.TestHeader(t, r, "Accept", "application/json")
   369  		th.TestJSONRequest(t, r, `
   370  {
   371      "firewall_rule_id": "9fed8075-06ee-463f-83a6-d4118791b02f"
   372  }
   373          `)
   374  
   375  		w.Header().Add("Content-Type", "application/json")
   376  		w.WriteHeader(http.StatusOK)
   377  
   378  		fmt.Fprintf(w, `
   379  {
   380      "audited": false,
   381      "description": "TESTACC-DESC-skno2e52",
   382      "firewall_rules": [
   383        "3ccc0f2b-4a04-4e7c-bb47-dd1701127a47"
   384      ],
   385      "id": "9fed8075-06ee-463f-83a6-d4118791b02f",
   386      "name": "TESTACC-Qf7pMSkq",
   387      "project_id": "TESTID-era34jkaslk",
   388      "shared": false,
   389      "tenant_id": "TESTID-334sdfassdf"
   390  }
   391      `)
   392  	})
   393  
   394  	policy, err := policies.RemoveRule(fake.ServiceClient(), "9fed8075-06ee-463f-83a6-d4118791b02f", "9fed8075-06ee-463f-83a6-d4118791b02f").Extract()
   395  	th.AssertNoErr(t, err)
   396  	th.AssertEquals(t, "9fed8075-06ee-463f-83a6-d4118791b02f", policy.ID)
   397  
   398  }