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

providerfwk: adjust code (show, set, delete) lines in same way #671

Merged
merged 2 commits into from
Jun 17, 2024
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
4 changes: 1 addition & 3 deletions internal/junos/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ const (
PipeDisplaySet = " | display set"
PipeDisplaySetRelative = PipeDisplaySet + " relative"

RoutingInstancesWS = "routing-instances " // routing-instances word + space
SetRoutingInstances = SetLS + RoutingInstancesWS
DelRoutingInstances = DeleteLS + RoutingInstancesWS
RoutingInstancesWS = "routing-instances " // routing-instances word + space

RoutingOptionsWS = "routing-options "
RibInet60WS = "rib inet6.0 "
Expand Down
21 changes: 11 additions & 10 deletions internal/providerfwk/resource_aggregate_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func (rsc *aggregateRoute) ImportState(
func checkAggregateRouteExists(
_ context.Context, destination, routingInstance string, junSess *junos.Session,
) (
_ bool, err error,
bool, error,
) {
showPrefix := junos.CmdShowConfig
switch routingInstance {
Expand All @@ -508,11 +508,11 @@ func checkAggregateRouteExists(
showPrefix += "rib " + routingInstance + ".inet6.0 "
}
}
showConfig, err := junSess.Command(showPrefix + "aggregate route " + destination + junos.PipeDisplaySet)
showConfig, err := junSess.Command(showPrefix +
"aggregate route " + destination + junos.PipeDisplaySet)
if err != nil {
return false, err
}

if showConfig == junos.EmptyW {
return false, nil
}
Expand Down Expand Up @@ -551,6 +551,7 @@ func (rscData *aggregateRouteData) set(
}
}
setPrefix += "aggregate route " + rscData.Destination.ValueString() + " "

configSet := []string{
setPrefix,
}
Expand Down Expand Up @@ -603,9 +604,7 @@ func (rscData *aggregateRouteData) set(

func (rscData *aggregateRouteData) read(
_ context.Context, destination, routingInstance string, junSess *junos.Session,
) (
err error,
) {
) error {
showPrefix := junos.CmdShowConfig
switch routingInstance {
case junos.DefaultW, "":
Expand All @@ -619,16 +618,17 @@ func (rscData *aggregateRouteData) read(
showPrefix += "rib " + routingInstance + ".inet6.0 "
}
}
showConfig, err := junSess.Command(showPrefix + "aggregate route " + destination + junos.PipeDisplaySetRelative)
showConfig, err := junSess.Command(showPrefix +
"aggregate route " + destination + junos.PipeDisplaySetRelative)
if err != nil {
return err
}

if showConfig != junos.EmptyW {
rscData.Destination = types.StringValue(destination)
rscData.RoutingInstance = types.StringValue(routingInstance)
if rscData.RoutingInstance.ValueString() == "" {
if routingInstance == "" {
rscData.RoutingInstance = types.StringValue(junos.DefaultW)
} else {
rscData.RoutingInstance = types.StringValue(routingInstance)
}
rscData.fillID()
for _, item := range strings.Split(showConfig, "\n") {
Expand Down Expand Up @@ -700,6 +700,7 @@ func (rscData *aggregateRouteData) del(
delPrefix += "rib " + routingInstance + ".inet6.0 "
}
}

configSet := []string{
delPrefix + "aggregate route " + rscData.Destination.ValueString(),
}
Expand Down
5 changes: 2 additions & 3 deletions internal/providerfwk/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ func (block *applicationBlockTerm) configSet(
error, // error
) {
setPrefix += "term " + block.Name.ValueString() + " "

configSet := []string{
setPrefix,
setPrefix + "protocol " + block.Protocol.ValueString(),
Expand Down Expand Up @@ -742,9 +743,7 @@ func (block *applicationBlockTerm) configSet(

func (rscData *applicationData) read(
_ context.Context, name string, junSess *junos.Session,
) (
err error,
) {
) error {
showConfig, err := junSess.Command(junos.CmdShowConfig +
"applications application " + name + junos.PipeDisplaySetRelative)
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions internal/providerfwk/resource_application_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,7 @@ func (rscData *applicationSetData) set(

func (rscData *applicationSetData) read(
_ context.Context, name string, junSess *junos.Session,
) (
err error,
) {
) error {
showConfig, err := junSess.Command(junos.CmdShowConfig +
"applications application-set " + name + junos.PipeDisplaySetRelative)
if err != nil {
Expand Down
91 changes: 39 additions & 52 deletions internal/providerfwk/resource_bgp_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,15 +888,15 @@ func (rsc *bgpGroup) ValidateConfig(
resp.Diagnostics.AddAttributeError(
path.Root("advertise_peer_as"),
tfdiag.ConflictConfigErrSummary,
"advertise_peer_as and no_advertise_peer_as can't be true in same time ",
"advertise_peer_as and no_advertise_peer_as can't be true in same time",
)
}
if !config.KeepAll.IsNull() && !config.KeepAll.IsUnknown() &&
!config.KeepNone.IsNull() && !config.KeepNone.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("keep_all"),
tfdiag.ConflictConfigErrSummary,
"keep_all and keep_none can't be true in same time ",
"keep_all and keep_none can't be true in same time",
)
}
if !config.AuthenticationKey.IsNull() && !config.AuthenticationKey.IsUnknown() {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ func (rsc *bgpGroup) ValidateConfig(
path.Root("family_evpn").AtListIndex(i).AtName("accepted_prefix_limit").AtName("teardown_idle_timeout"),
tfdiag.ConflictConfigErrSummary,
"teardown_idle_timeout and teardown_idle_timeout_forever cannot be configured together"+
" in accepted_prefix_limit block in family_evpn block ",
" in accepted_prefix_limit block in family_evpn block",
)
}
}
Expand All @@ -1104,7 +1104,7 @@ func (rsc *bgpGroup) ValidateConfig(
path.Root("family_evpn").AtListIndex(i).AtName("prefix_limit").AtName("teardown_idle_timeout"),
tfdiag.ConflictConfigErrSummary,
"teardown_idle_timeout and teardown_idle_timeout_forever cannot be configured together"+
" in prefix_limit block family_evpn block ",
" in prefix_limit block family_evpn block",
)
}
}
Expand Down Expand Up @@ -1147,7 +1147,7 @@ func (rsc *bgpGroup) ValidateConfig(
path.Root("family_inet").AtListIndex(i).AtName("accepted_prefix_limit").AtName("teardown_idle_timeout"),
tfdiag.ConflictConfigErrSummary,
"teardown_idle_timeout and teardown_idle_timeout_forever cannot be configured together"+
" in accepted_prefix_limit block in family_inet block ",
" in accepted_prefix_limit block in family_inet block",
)
}
}
Expand All @@ -1167,7 +1167,7 @@ func (rsc *bgpGroup) ValidateConfig(
path.Root("family_inet").AtListIndex(i).AtName("prefix_limit").AtName("teardown_idle_timeout"),
tfdiag.ConflictConfigErrSummary,
"teardown_idle_timeout and teardown_idle_timeout_forever cannot be configured together"+
" in prefix_limit block family_inet block ",
" in prefix_limit block family_inet block",
)
}
}
Expand Down Expand Up @@ -1210,7 +1210,7 @@ func (rsc *bgpGroup) ValidateConfig(
path.Root("family_inet6").AtListIndex(i).AtName("accepted_prefix_limit").AtName("teardown_idle_timeout"),
tfdiag.ConflictConfigErrSummary,
"teardown_idle_timeout and teardown_idle_timeout_forever cannot be configured together"+
" in accepted_prefix_limit block in family_inet6 block ",
" in accepted_prefix_limit block in family_inet6 block",
)
}
}
Expand All @@ -1230,7 +1230,7 @@ func (rsc *bgpGroup) ValidateConfig(
path.Root("family_inet6").AtListIndex(i).AtName("prefix_limit").AtName("teardown_idle_timeout"),
tfdiag.ConflictConfigErrSummary,
"teardown_idle_timeout and teardown_idle_timeout_forever cannot be configured together"+
" in prefix_limit block family_inet6 block ",
" in prefix_limit block family_inet6 block",
)
}
}
Expand Down Expand Up @@ -1537,22 +1537,16 @@ func checkBgpGroupExists(
routingInstance string,
junSess *junos.Session,
) (
_ bool, err error,
bool, error,
) {
var showConfig string
if routingInstance == "" || routingInstance == junos.DefaultW {
showConfig, err = junSess.Command(junos.CmdShowConfig +
"protocols bgp group \"" + name + "\"" + junos.PipeDisplaySet)
if err != nil {
return false, err
}
} else {
showConfig, err = junSess.Command(junos.CmdShowConfig +
junos.RoutingInstancesWS + routingInstance + " " +
"protocols bgp group \"" + name + "\"" + junos.PipeDisplaySet)
if err != nil {
return false, err
}
showPrefix := junos.CmdShowConfig
if routingInstance != "" && routingInstance != junos.DefaultW {
showPrefix += junos.RoutingInstancesWS + routingInstance + " "
}
showConfig, err := junSess.Command(showPrefix +
"protocols bgp group \"" + name + "\"" + junos.PipeDisplaySet)
if err != nil {
return false, err
}
if showConfig == junos.EmptyW {
return false, nil
Expand All @@ -1578,11 +1572,12 @@ func (rscData *bgpGroupData) set(
) (
path.Path, error,
) {
setPrefix := "set protocols bgp group \"" + rscData.Name.ValueString() + "\" "
setPrefix := junos.SetLS
if v := rscData.RoutingInstance.ValueString(); v != "" && v != junos.DefaultW {
setPrefix = junos.SetRoutingInstances + v +
" protocols bgp group \"" + rscData.Name.ValueString() + "\" "
setPrefix += junos.RoutingInstancesWS + v + " "
}
setPrefix += "protocols bgp group \"" + rscData.Name.ValueString() + "\" "

configSet := []string{
setPrefix + "type " + rscData.Type.ValueString(),
}
Expand Down Expand Up @@ -1789,23 +1784,15 @@ func (rscData *bgpGroupData) set(

func (rscData *bgpGroupData) read(
_ context.Context, name, routingInstance string, junSess *junos.Session,
) (
err error,
) {
var showConfig string
if routingInstance == "" || routingInstance == junos.DefaultW {
showConfig, err = junSess.Command(junos.CmdShowConfig +
"protocols bgp group \"" + name + "\"" + junos.PipeDisplaySetRelative)
if err != nil {
return err
}
} else {
showConfig, err = junSess.Command(junos.CmdShowConfig +
junos.RoutingInstancesWS + routingInstance + " " +
"protocols bgp group \"" + name + "\"" + junos.PipeDisplaySetRelative)
if err != nil {
return err
}
) error {
showPrefix := junos.CmdShowConfig
if routingInstance != "" && routingInstance != junos.DefaultW {
showPrefix += junos.RoutingInstancesWS + routingInstance + " "
}
showConfig, err := junSess.Command(showPrefix +
"protocols bgp group \"" + name + "\"" + junos.PipeDisplaySetRelative)
if err != nil {
return err
}
if showConfig != junos.EmptyW {
rscData.Name = types.StringValue(name)
Expand Down Expand Up @@ -2020,11 +2007,11 @@ func (rscData *bgpGroupData) delOpts(
_ context.Context, junSess *junos.Session,
) error {
configSet := make([]string, 0)
delPrefix := "delete protocols bgp group \"" + rscData.Name.ValueString() + "\" "
delPrefix := junos.DeleteLS
if v := rscData.RoutingInstance.ValueString(); v != "" && v != junos.DefaultW {
delPrefix = junos.DelRoutingInstances + v +
" protocols bgp group \"" + rscData.Name.ValueString() + "\" "
delPrefix += junos.RoutingInstancesWS + v + " "
}
delPrefix += "protocols bgp group \"" + rscData.Name.ValueString() + "\" "

configSet = append(configSet,
delPrefix+"accept-remote-nexthop",
Expand Down Expand Up @@ -2073,13 +2060,13 @@ func (rscData *bgpGroupData) delOpts(
func (rscData *bgpGroupData) del(
_ context.Context, junSess *junos.Session,
) error {
configSet := make([]string, 1)
delPrefix := junos.DeleteLS
if v := rscData.RoutingInstance.ValueString(); v != "" && v != junos.DefaultW {
configSet[0] = junos.DelRoutingInstances + v +
" protocols bgp group \"" + rscData.Name.ValueString() + "\""
} else {
configSet[0] = junos.DeleteW +
" protocols bgp group \"" + rscData.Name.ValueString() + "\""
delPrefix += junos.RoutingInstancesWS + v + " "
}

configSet := []string{
delPrefix + "protocols bgp group \"" + rscData.Name.ValueString() + "\"",
}

return junSess.ConfigSet(configSet)
Expand Down
Loading
Loading