Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add firewall rule import #795

Open
0xalex88 opened this issue Jan 3, 2025 · 1 comment
Open

[FEATURE] Add firewall rule import #795

0xalex88 opened this issue Jan 3, 2025 · 1 comment

Comments

@0xalex88
Copy link

0xalex88 commented Jan 3, 2025

Description

When migrating to terraform it would be nice to import firewall rules to avoid having to delete and recreate them

Affected Resource(s) and/or Data Source(s)

  • ovh_ip_firewall_rule

Potential Terraform Configuration

Something like this:

data "ovh_ip_firewall_rule" "rpc_http" {
  for_each       = data.ovh_dedicated_server.rpc_node
  ip             = each.value.ip
  ip_on_firewall = each.value.ip
  sequence       = 1
}

import {
  for_each = data.ovh_ip_firewall_rule.rpc_http
  to       = ovh_ip_firewall_rule.rpc_http[each.key]
  id       = "${each.value.ip}/${each.value.ip}/${each.value.sequence}"
}

resource "ovh_ip_firewall_rule" "rpc_http" {
  for_each         = data.ovh_dedicated_server.rpc_node
  ip               = each.value.ip
  ip_on_firewall   = each.value.ip
  sequence         = 1
  action           = "permit"
  protocol         = "tcp"
  destination_port = 80
}

Additional context

I've implemented this code in resource_ip_firewall_rule.go that worked for us:

func (r *ipFirewallRuleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
	splits := strings.Split(req.ID, "/")
	if len(splits) < 3 {
		resp.Diagnostics.AddError("Given ID is malformed", "ID must be formatted like the following: <ip>/<ipOnFirewall>/<sequence>")
		return
	}

	ip := splits[0]
	ipOnFirewall := splits[1]
	sequence, err := strconv.ParseFloat(splits[2], 64)
	if err != nil {
		resp.Diagnostics.AddError("Given firewall sequence number is malformed", "Sequence must be a numeric value")
		return
	}

	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("ip"), ip)...)
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("ip_on_firewall"), ipOnFirewall)...)
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("sequence"), sequence)...)
}

then I had to edit the Read function to read the DestinationPort from DestinationPortDesc (it might probably need to do the same forse sourcePort and/or other fields):

         if strings.HasPrefix(responseData.DestinationPortDesc.ValueString(), "eq ") {
		port := strings.TrimPrefix(responseData.DestinationPortDesc.ValueString(), "eq ")
		portNumber, err := (strconv.ParseInt(port, 10, 64))
		if err == nil {
			responseData.DestinationPort = ovhtypes.NewTfInt64Value(portNumber)
		}
	}

	responseData.MergeWith(&data)
@amstuta
Copy link
Contributor

amstuta commented Jan 6, 2025

Hello @0xalex88, thanks for opening this issue and for your code proposal :)

We'll add this quickly !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants