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

Refactor/bgp peer #180

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 74 additions & 97 deletions client/bgp_peer.go
Original file line number Diff line number Diff line change
@@ -1,135 +1,112 @@
package client

import (
"fmt"
"log"
"github.com/go-routeros/routeros"
)

// BgpPeer Mikrotik resource
type BgpPeer struct {
ID string `mikrotik:".id"`
Name string `mikrotik:"name"`
AddressFamilies string `mikrotik:"address-families"`
AllowAsIn int `mikrotik:"allow-as-in"`
AsOverride bool `mikrotik:"as-override"`
CiscoVplsNlriLenFmt string `mikrotik:"cisco-vpls-nlri-len-fmt"`
Comment string `mikrotik:"comment"`
DefaultOriginate string `mikrotik:"default-originate"`
Disabled bool `mikrotik:"disabled"`
HoldTime string `mikrotik:"hold-time"`
InFilter string `mikrotik:"in-filter"`
Instance string `mikrotik:"instance"`
KeepAliveTime string `mikrotik:"keepalive-time"`
MaxPrefixLimit int `mikrotik:"max-prefix-limit"`
MaxPrefixRestartTime string `mikrotik:"max-prefix-restart-time"`
Multihop bool `mikrotik:"multihop"`
NexthopChoice string `mikrotik:"nexthop-choice"`
OutFilter string `mikrotik:"out-filter"`
Passive bool `mikrotik:"passive"`
RemoteAddress string `mikrotik:"remote-address"`
RemoteAs int `mikrotik:"remote-as"`
RemotePort int `mikrotik:"remote-port"`
RemovePrivateAs bool `mikrotik:"remove-private-as"`
RouteReflect bool `mikrotik:"route-reflect"`
TCPMd5Key string `mikrotik:"tcp-md5-key"`
TTL string `mikrotik:"ttl"`
UpdateSource string `mikrotik:"update-source"`
UseBfd bool `mikrotik:"use-bfd"`
Id string `mikrotik:".id" codegen:"id,mikrotikID"`
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change also breaks the client module

Name string `mikrotik:"name" codegen:"name,required,terraformID"`
AddressFamilies string `mikrotik:"address-families" codegen:"address_families,optional,computed"`
AllowAsIn int `mikrotik:"allow-as-in" codegen:"allow_as_in"`
AsOverride bool `mikrotik:"as-override" codegen:"as_override"`
CiscoVplsNlriLenFmt string `mikrotik:"cisco-vpls-nlri-len-fmt" codegen:"cisco_vpls_nlri_len_fmt"`
Comment string `mikrotik:"comment" codegen:"comment"`
DefaultOriginate string `mikrotik:"default-originate" codegen:"default_originate,optional,computed"`
Disabled bool `mikrotik:"disabled" codegen:"disabled"`
HoldTime string `mikrotik:"hold-time" codegen:"hold_time,optional,computed"`
InFilter string `mikrotik:"in-filter" codegen:"in_filter"`
Instance string `mikrotik:"instance" codegen:"instance"`
KeepAliveTime string `mikrotik:"keepalive-time" codegen:"keepalive_time"`
MaxPrefixLimit int `mikrotik:"max-prefix-limit" codegen:"max_prefix_limit"`
MaxPrefixRestartTime string `mikrotik:"max-prefix-restart-time" codegen:"max_prefix_restart_time"`
Multihop bool `mikrotik:"multihop" codegen:"multihop"`
NexthopChoice string `mikrotik:"nexthop-choice" codegen:"nexthop_choice,optional,computed"`
OutFilter string `mikrotik:"out-filter" codegen:"out_filter"`
Passive bool `mikrotik:"passive" codegen:"passive"`
RemoteAddress string `mikrotik:"remote-address" codegen:"remote_address,required"`
RemoteAs int `mikrotik:"remote-as" codegen:"remote_as,required"`
RemotePort int `mikrotik:"remote-port" codegen:"remote_port"`
RemovePrivateAs bool `mikrotik:"remove-private-as" codegen:"remove_private_as"`
RouteReflect bool `mikrotik:"route-reflect" codegen:"route_reflect"`
TCPMd5Key string `mikrotik:"tcp-md5-key" codegen:"tcp_md5_key"`
TTL string `mikrotik:"ttl" codegen:"ttl,optional,computed"`
UpdateSource string `mikrotik:"update-source" codegen:"update_source"`
UseBfd bool `mikrotik:"use-bfd" codegen:"use_bfd"`
}

func (client Mikrotik) AddBgpPeer(b *BgpPeer) (*BgpPeer, error) {
c, err := client.getMikrotikClient()
var _ Resource = (*BgpPeer)(nil)

if err != nil {
return nil, err
}

cmd := Marshal("/routing/bgp/peer/add", b)
func (b *BgpPeer) ActionToCommand(a Action) string {
return map[Action]string{
Add: "/routing/bgp/peer/add",
Find: "/routing/bgp/peer/print",
Update: "/routing/bgp/peer/set",
Delete: "/routing/bgp/peer/remove",
}[a]
}

log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)
log.Printf("[DEBUG] /routing/bgp/peer/add returned %v", r)
if err != nil {
if legacyBgpUnsupported(err) {
return nil, LegacyBgpUnsupported{}
}
return nil, err
}
func (b *BgpPeer) IDField() string {
return ".id"
}

return client.FindBgpPeer(b.Name)
func (b *BgpPeer) ID() string {
return b.Id
}

func (client Mikrotik) FindBgpPeer(name string) (*BgpPeer, error) {
c, err := client.getMikrotikClient()
if err != nil {
return nil, err
}
func (b *BgpPeer) SetID(id string) {
b.Id = id
}

cmd := []string{"/routing/bgp/peer/print", "?name=" + name}
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)
func (b *BgpPeer) AfterAddHook(r *routeros.Reply) {
b.Id = r.Done.Map["ret"]
}

if err != nil {
if legacyBgpUnsupported(err) {
return nil, LegacyBgpUnsupported{}
}
return nil, err
}
func (b *BgpPeer) FindField() string {
return "name"
}

log.Printf("[DEBUG] Find bgp peer: `%v`", cmd)
func (b *BgpPeer) FindFieldValue() string {
return b.Name
}

bgpPeer := BgpPeer{}
func (b *BgpPeer) DeleteField() string {
return "numbers"
}

err = Unmarshal(*r, &bgpPeer)
func (b *BgpPeer) DeleteFieldValue() string {
return b.Name
}

// Typed wrappers
func (c Mikrotik) AddBgpPeer(r *BgpPeer) (*BgpPeer, error) {
res, err := c.Add(r)
if err != nil {
return nil, err
}

if bgpPeer.Name == "" {
return nil, NewNotFound(fmt.Sprintf("bgp peer `%s` not found", name))
}

return &bgpPeer, nil
return res.(*BgpPeer), nil
}

func (client Mikrotik) UpdateBgpPeer(b *BgpPeer) (*BgpPeer, error) {
c, err := client.getMikrotikClient()

func (c Mikrotik) UpdateBgpPeer(r *BgpPeer) (*BgpPeer, error) {
res, err := c.Update(r)
if err != nil {
return nil, err
}

cmd := Marshal("/routing/bgp/peer/set", b)

log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
_, err = c.RunArgs(cmd)
return res.(*BgpPeer), nil
}

func (c Mikrotik) FindBgpPeer(name string) (*BgpPeer, error) {
res, err := c.Find(&BgpPeer{Name: name})
if err != nil {
if legacyBgpUnsupported(err) {
return nil, LegacyBgpUnsupported{}
}
return nil, err
}

return client.FindBgpPeer(b.Name)
return res.(*BgpPeer), nil
}

func (client Mikrotik) DeleteBgpPeer(name string) error {
c, err := client.getMikrotikClient()
if err != nil {
return err
}

bgpPeer, err := client.FindBgpPeer(name)
if err != nil {
return err
}

cmd := []string{"/routing/bgp/peer/remove", "=numbers=" + bgpPeer.Name}
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)
log.Printf("[DEBUG] Remove bgp peer via mikrotik api: %v", r)

return err
func (c Mikrotik) DeleteBgpPeer(name string) error {
return c.Delete(&BgpPeer{Name: name})
}
4 changes: 2 additions & 2 deletions client/bgp_peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestAddBgpPeerAndDeleteBgpPeer(t *testing.T) {
t.Fatalf("Error creating a bpg peer with: %v", err)
}

expectedBgpPeer.ID = bgpPeer.ID
expectedBgpPeer.Id = bgpPeer.Id

if !reflect.DeepEqual(bgpPeer, expectedBgpPeer) {
t.Errorf("The bgp peer does not match what we expected. actual: %v expected: %v", bgpPeer, expectedBgpPeer)
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestAddAndUpdateBgpPeerWithOptionalFieldsAndDeleteBgpPeer(t *testing.T) {
t.Fatalf("Error creating a bpg peer with: %v", err)
}

expectedBgpPeer.ID = bgpPeer.ID
expectedBgpPeer.Id = bgpPeer.Id

if !reflect.DeepEqual(bgpPeer, expectedBgpPeer) {
t.Errorf("The bgp peer does not match what we expected. actual: %v expected: %v", bgpPeer, expectedBgpPeer)
Expand Down
28 changes: 14 additions & 14 deletions docs/resources/bgp_peer.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,40 @@ resource "mikrotik_bgp_peer" "peer" {

### Required

- `instance` (String) The name of the instance this peer belongs to. See Mikrotik bgp instance resource.
- `name` (String) The name of the BGP peer.
- `remote_address` (String) The address of the remote peer
- `remote_as` (Number) The 32-bit AS number of the remote peer.

### Optional

- `address_families` (String) The list of address families about which this peer will exchange routing information. Default: `ip`.
- `address_families` (String) The list of address families about which this peer will exchange routing information.
- `allow_as_in` (Number) How many times to allow own AS number in AS-PATH, before discarding a prefix.
- `as_override` (Boolean) If set, then all instances of remote peer's AS number in BGP AS PATH attribute are replaced with local AS number before sending route update to that peer. Default: `false`.
- `as_override` (Boolean) If set, then all instances of remote peer's AS number in BGP AS PATH attribute are replaced with local AS number before sending route update to that peer.
- `cisco_vpls_nlri_len_fmt` (String) VPLS NLRI length format type.
- `comment` (String) The comment of the BGP peer to be created.
- `default_originate` (String) The comment of the BGP peer to be created. Default: `never`.
- `disabled` (Boolean) Whether peer is disabled. Default: `false`.
- `hold_time` (String) Specifies the BGP Hold Time value to use when negotiating with peer Default: `3m`.
- `default_originate` (String) The comment of the BGP peer to be created.
- `disabled` (Boolean) Whether peer is disabled.
- `hold_time` (String) Specifies the BGP Hold Time value to use when negotiating with peer
- `in_filter` (String) The name of the routing filter chain that is applied to the incoming routing information.
- `instance` (String) The name of the instance this peer belongs to. See Mikrotik bgp instance resource.
- `keepalive_time` (String)
- `max_prefix_limit` (Number) Maximum number of prefixes to accept from a specific peer.
- `max_prefix_restart_time` (String) Minimum time interval after which peers can reestablish BGP session.
- `multihop` (Boolean) Specifies whether the remote peer is more than one hop away. Default: `false`.
- `nexthop_choice` (String) Affects the outgoing NEXT_HOP attribute selection, either: 'default', 'force-self', or 'propagate' Default: `default`.
- `multihop` (Boolean) Specifies whether the remote peer is more than one hop away.
- `nexthop_choice` (String) Affects the outgoing NEXT_HOP attribute selection, either: 'default', 'force-self', or 'propagate'
- `out_filter` (String) The name of the routing filter chain that is applied to the outgoing routing information.
- `passive` (Boolean) Name of the routing filter chain that is applied to the outgoing routing information. Default: `false`.
- `passive` (Boolean) Name of the routing filter chain that is applied to the outgoing routing information.
- `remote_port` (Number) Remote peers port to establish tcp session.
- `remove_private_as` (Boolean) If set, then BGP AS-PATH attribute is removed before sending out route update if attribute contains only private AS numbers. Default: `false`.
- `route_reflect` (Boolean) Specifies whether this peer is route reflection client. Default: `false`.
- `remove_private_as` (Boolean) If set, then BGP AS-PATH attribute is removed before sending out route update if attribute contains only private AS numbers.
- `route_reflect` (Boolean) Specifies whether this peer is route reflection client.
- `tcp_md5_key` (String) Key used to authenticate the connection with TCP MD5 signature as described in RFC 2385.
- `ttl` (String) Time To Live, the hop limit for TCP connection. This is a `string` field that can be 'default' or '0'-'255'. Default: `default`.
- `ttl` (String) Time To Live, the hop limit for TCP connection. This is a `string` field that can be 'default' or '0'-'255'.
- `update_source` (String) If address is specified, this address is used as the source address of the outgoing TCP connection.
- `use_bfd` (Boolean) Whether to use BFD protocol for fast state detection. Default: `false`.
- `use_bfd` (Boolean) Whether to use BFD protocol for fast state detection.

### Read-Only

- `id` (String) The ID of this resource.
- `id` (String) Unique MikroTik identifier.

## Import
Import is supported using the following syntax:
Expand Down
1 change: 0 additions & 1 deletion mikrotik/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func Provider(client *mt.Mikrotik) *schema.Provider {
},
ResourcesMap: map[string]*schema.Resource{
"mikrotik_bgp_instance": resourceBgpInstance(),
"mikrotik_bgp_peer": resourceBgpPeer(),
"mikrotik_bridge": resourceBridge(),
"mikrotik_bridge_port": resourceBridgePort(),
"mikrotik_bridge_vlan": resourceBridgeVlan(),
Expand Down
3 changes: 2 additions & 1 deletion mikrotik/provider_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ func (p *ProviderFramework) DataSources(ctx context.Context) []func() datasource

func (p *ProviderFramework) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewSchedulerResource,
NewBgpPeerResource,
NewInterfaceWireguardResource,
NewSchedulerResource,
NewScriptResource,
}
}
Expand Down
Loading