Ejemplo n.º 1
0
func TestRuleCRUD(t *testing.T) {
	client, err := clients.NewNetworkV2Client()
	if err != nil {
		t.Fatalf("Unable to create a network client: %v", err)
	}

	rule, err := CreateRule(t, client)
	if err != nil {
		t.Fatalf("Unable to create rule: %v", err)
	}
	defer DeleteRule(t, client, rule.ID)

	PrintRule(t, rule)

	ruleDescription := "Some rule description"
	updateOpts := rules.UpdateOpts{
		Description: &ruleDescription,
	}

	_, err = rules.Update(client, rule.ID, updateOpts).Extract()
	if err != nil {
		t.Fatalf("Unable to update rule: %v", err)
	}

	newRule, err := rules.Get(client, rule.ID).Extract()
	if err != nil {
		t.Fatalf("Unable to get rule: %v", err)
	}

	PrintRule(t, newRule)
}
func resourceFWRuleV1Update(d *schema.ResourceData, meta interface{}) error {
	config := meta.(*Config)
	networkingClient, err := config.networkingV2Client(d.Get("region").(string))
	if err != nil {
		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
	}

	opts := rules.UpdateOpts{}

	if d.HasChange("name") {
		v := d.Get("name").(string)
		opts.Name = &v
	}

	if d.HasChange("description") {
		v := d.Get("description").(string)
		opts.Description = &v
	}

	if d.HasChange("protocol") {
		v := d.Get("protocol").(string)
		opts.Protocol = &v
	}

	if d.HasChange("action") {
		v := d.Get("action").(string)
		opts.Action = &v
	}

	if d.HasChange("ip_version") {
		v := d.Get("ip_version").(int)
		ipVersion := resourceFWRuleV1DetermineIPVersion(v)
		opts.IPVersion = &ipVersion
	}

	if d.HasChange("source_ip_address") {
		v := d.Get("source_ip_address").(string)
		opts.SourceIPAddress = &v
	}

	if d.HasChange("destination_ip_address") {
		v := d.Get("destination_ip_address").(string)
		opts.DestinationIPAddress = &v
	}

	if d.HasChange("source_port") {
		v := d.Get("source_port").(string)
		opts.SourcePort = &v
	}

	if d.HasChange("destination_port") {
		v := d.Get("destination_port").(string)
		opts.DestinationPort = &v
	}

	if d.HasChange("enabled") {
		v := d.Get("enabled").(bool)
		opts.Enabled = &v
	}

	log.Printf("[DEBUG] Updating firewall rules: %#v", opts)

	err = rules.Update(networkingClient, d.Id(), opts).Err
	if err != nil {
		return err
	}

	return resourceFWRuleV1Read(d, meta)
}
Ejemplo n.º 3
0
func TestUpdate(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/v2.0/fw/firewall_rules/f03bd950-6c56-4f5e-a307-45967078f507", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "PUT")
		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
		th.TestHeader(t, r, "Content-Type", "application/json")
		th.TestHeader(t, r, "Accept", "application/json")
		th.TestJSONRequest(t, r, `
{
	"firewall_rule":{
		"protocol": "tcp",
		"description": "ssh rule",
		"destination_ip_address": "192.168.1.0/24",
		"destination_port": "22",
		"name": "ssh_form_any",
		"action": "allow",
		"enabled": false
	}
}
	`)

		w.Header().Add("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)

		fmt.Fprintf(w, `
{
	"firewall_rule":{
		"protocol": "tcp",
		"description": "ssh rule",
		"destination_ip_address": "192.168.1.0/24",
		"firewall_policy_id": "e2a5fb51-698c-4898-87e8-f1eee6b50919",
		"position": 2,
		"destination_port": "22",
		"id": "f03bd950-6c56-4f5e-a307-45967078f507",
		"name": "ssh_form_any",
		"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
		"enabled": false,
		"action": "allow",
		"ip_version": 4,
		"shared": false
	}
}
		`)
	})

	newProtocol := "tcp"
	newDescription := "ssh rule"
	newDestinationIP := "192.168.1.0/24"
	newDestintionPort := "22"
	newName := "ssh_form_any"
	newAction := "allow"

	options := rules.UpdateOpts{
		Protocol:             &newProtocol,
		Description:          &newDescription,
		DestinationIPAddress: &newDestinationIP,
		DestinationPort:      &newDestintionPort,
		Name:                 &newName,
		Action:               &newAction,
		Enabled:              gophercloud.Disabled,
	}

	_, err := rules.Update(fake.ServiceClient(), "f03bd950-6c56-4f5e-a307-45967078f507", options).Extract()
	th.AssertNoErr(t, err)
}