Skip to content

Commit

Permalink
adding vendor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RavinderReddyF5 committed Jul 30, 2024
1 parent 8e2ac73 commit 8988319
Show file tree
Hide file tree
Showing 12 changed files with 481 additions and 84 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/terraform-plugin-log v0.8.0
github.com/hashicorp/terraform-plugin-testing v1.2.0
github.com/stretchr/testify v1.8.4
gitswarm.f5net.com/terraform-providers/f5osclient v1.0.5
gitswarm.f5net.com/terraform-providers/f5osclient v1.0.7
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6e
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zclconf/go-cty v1.13.1 h1:0a6bRwuiSHtAmqCqNOE+c2oHgepv0ctoxU4FUe43kwc=
github.com/zclconf/go-cty v1.13.1/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
gitswarm.f5net.com/terraform-providers/f5osclient v1.0.5 h1:BAHOti8Jy0N/B6z8apUpYteCA5K11zcE5W8yHQHiWOc=
gitswarm.f5net.com/terraform-providers/f5osclient v1.0.5/go.mod h1:k+4tg9l6yO3FlFh3dkXGc+Wl+LOB/AXu302hAX6nhDU=
gitswarm.f5net.com/terraform-providers/f5osclient v1.0.7 h1:46YL6qdeG0lOkZwZH+nVAOspoEmQSl+N79p9hUL7JvU=
gitswarm.f5net.com/terraform-providers/f5osclient v1.0.7/go.mod h1:k+4tg9l6yO3FlFh3dkXGc+Wl+LOB/AXu302hAX6nhDU=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
12 changes: 1 addition & 11 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type F5osProviderModel struct {
Port types.Int64 `tfsdk:"port"`
TeemDisable types.Bool `tfsdk:"teem_disable"`
DisableSslVerify types.Bool `tfsdk:"disable_tls_verify"`
// TrustedCertpath types.String `tfsdk:"trusted_cert_path"`
}
type TeemData struct {
ResourceName string
Expand Down Expand Up @@ -74,10 +73,6 @@ func (p *F5osProvider) Schema(ctx context.Context, req provider.SchemaRequest, r
MarkdownDescription: "Port Number to be used to make API calls to HOST",
Optional: true,
},
// "trusted_cert_path": schema.StringAttribute{
// MarkdownDescription: "Valid Trusted CA Certificate path",
// Optional: true,
// },
"disable_tls_verify": schema.BoolAttribute{
MarkdownDescription: "`disable_tls_verify` controls whether a client verifies the server's certificate chain and host name. default it is set to `true`. If `disable_tls_verify` is true, crypto/tls accepts any certificate presented by the server and any host name in that certificate. In this mode, TLS is susceptible to machine-in-the-middle attacks unless custom verification is used.\ncan be provided by `DISABLE_TLS_VERIFY` environment variable.\n\n~> **NOTE** If it is set to `false`, certificate/ca certificates should be added to `trusted store` of host where we are running this provider.",
Optional: true,
Expand Down Expand Up @@ -181,12 +176,7 @@ func (p *F5osProvider) Configure(ctx context.Context, req provider.ConfigureRequ
}
client, err := f5ossdk.NewSession(f5osConfig)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create f5os Client",
"An unexpected error occurred when creating the f5os client connection."+
"If the error is not clear, please contact the provider developers.\n\n"+
"f5os Client Error: "+err.Error(),
)
resp.Diagnostics.AddError(fmt.Sprintf("%+v", err.Error()), "")
return
}
client.Teem = teemDisable
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/tenant_image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,14 @@ func (r *TenantImageResource) Create(ctx context.Context, req resource.CreateReq
if data.UploadFromPath.IsNull() {
respByte, err := r.importImage(ctx, data)
if err != nil {
resp.Diagnostics.AddError("F5OS Client Error:", fmt.Sprintf("Unable to Import Image, got error: %s", err))
resp.Diagnostics.AddError("[F5OS]Unable to Import Image:", fmt.Sprintf("%s", err))
return
}
if string(respByte) != "Import Image Transfer Success" {
resp.Diagnostics.AddError("Client Error", "Import Image failed")
resp.Diagnostics.AddError("[F5OS]", "Import Image failed")
return
}

} else {
respByte, err := r.uploadImage(ctx, data)
if err != nil {
Expand Down
84 changes: 58 additions & 26 deletions internal/provider/tenant_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand Down Expand Up @@ -109,7 +111,6 @@ func (r *TenantResource) Schema(ctx context.Context, req resource.SchemaRequest,
Validators: []validator.String{
stringvalidator.OneOf([]string{"one", "small", "medium", "large"}...),
},
// Default: stringdefault.StaticString("one"),
},
"dag_ipv6_prefix_length": schema.Int64Attribute{
MarkdownDescription: "Configuring DAG Global IPv6 Prefix Length,value Range from `1` to `128`.Default is `128`.",
Expand Down Expand Up @@ -227,9 +228,11 @@ func (r *TenantResource) Create(ctx context.Context, req resource.CreateRequest,
return
}
}
stop := r.client.F5OsKeepAlive(15 * time.Second)
imageObj, err := r.client.GetImage(data.ImageName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Unable to Get Image Details", fmt.Sprintf("Error:%s", err))
stop <- true
resp.Diagnostics.AddError(fmt.Sprintf("%v", err), "")
return
}
var availableFlag = true
Expand All @@ -239,11 +242,12 @@ func (r *TenantResource) Create(ctx context.Context, req resource.CreateRequest,
}
}
if !availableFlag {
resp.Diagnostics.AddError("Unable to Get Image Details", fmt.Sprintf("Get Image: %s failed with error:%s", data.ImageName.ValueString(), "not-present"))
stop <- true
resp.Diagnostics.AddError(fmt.Sprintf("%v", err), "")
return
}

tenantConfig := getTenantCreateConfig(ctx, req, resp)
tenantConfig := r.getTenantCreateConfig(ctx, req, resp)

if data.Type.ValueString() == "BIG-IP-Next" {
tenantConfig.F5TenantsTenant[0].Config.DeploymentFile = data.DeploymentFile.ValueString()
Expand All @@ -254,14 +258,20 @@ func (r *TenantResource) Create(ctx context.Context, req resource.CreateRequest,
teemInfo := make(map[string]interface{})
teemInfo["teemData"] = r.teemData
r.client.Metadata = teemInfo
err = r.client.SendTeem(teemInfo)
if err != nil {
resp.Diagnostics.AddError("Teem Error", fmt.Sprintf("Sending Teem Data failed: %s", err))
}
_ = r.client.SendTeem(teemInfo)
// if err != nil {
// resp.Diagnostics.AddError("Teem Error", fmt.Sprintf("Sending Teem Data failed: %s", err))
// }
tflog.Info(ctx, fmt.Sprintf("Timeout :%+v", int(data.Timeout.ValueInt64())))
respByte, err := r.client.CreateTenant(tenantConfig, int(data.Timeout.ValueInt64()))
if err != nil {
resp.Diagnostics.AddError("F5OS Client Error:", fmt.Sprintf("Tenant Deploy failed, got error: %s", err))
_ = r.client.DeleteTenant(data.Name.ValueString())
resp.Diagnostics.AddError(fmt.Sprintf("%v", err.Error()), "")
if strings.Contains(err.Error(), "400 Bad Request") {
stop <- true
return
}
// _ = r.client.DeleteTenant(data.Name.ValueString())
stop <- true
return
}
tflog.Info(ctx, fmt.Sprintf("tenantConfig Response:%+v", string(respByte)))
Expand All @@ -271,9 +281,11 @@ func (r *TenantResource) Create(ctx context.Context, req resource.CreateRequest,

respByte2, err := r.client.GetTenant(data.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError("F5OS Client Error", fmt.Sprintf("Unable to Read/Get Tenants, got error: %s", err))
stop <- true
resp.Diagnostics.AddError(fmt.Sprintf("%v", err.Error()), "")
return
}
stop <- true
tflog.Info(ctx, fmt.Sprintf("get tenantConfig :%+v", respByte2))
r.tenantResourceModeltoState(ctx, respByte2, data)
// mutex.Unlock()
Expand All @@ -292,11 +304,14 @@ func (r *TenantResource) Read(ctx context.Context, req resource.ReadRequest, res
return
}
//respByte, err := r.client.GetTenant(data.Name.ValueString())
stop := r.client.F5OsKeepAlive(15 * time.Second)
respByte, err := r.client.GetTenant(data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("F5OS Client Error", fmt.Sprintf("Unable to Read/Get Tenants, got error: %s", err))
stop <- true
resp.Diagnostics.AddError(fmt.Sprintf("%v", err.Error()), "")
return
}
stop <- true
r.tenantResourceModeltoState(ctx, respByte, data)

// Save updated data into Terraform state
Expand All @@ -312,25 +327,29 @@ func (r *TenantResource) Update(ctx context.Context, req resource.UpdateRequest,
if resp.Diagnostics.HasError() {
return
}
tenantConfig := getTenantUpdateConfig(ctx, req, resp)
tenantConfig := r.getTenantUpdateConfig(ctx, req, resp)

if data.Type.ValueString() == "BIG-IP-Next" {
tenantConfig.F5TenantsTenants.Tenant[0].Config.DeploymentFile = data.DeploymentFile.ValueString()
}
tflog.Info(ctx, fmt.Sprintf("[Update] tenantConfig :%+v", tenantConfig))
// mutex.Lock()
stop := r.client.F5OsKeepAlive(15 * time.Second)
respByte, err := r.client.UpdateTenant(tenantConfig, int(data.Timeout.ValueInt64()))
if err != nil {
stop <- true
resp.Diagnostics.AddError("F5OS Client Error:", fmt.Sprintf("Tenant Deploy failed, got error: %s", err))
return
}
tflog.Info(ctx, fmt.Sprintf("[Update] tenantConfig resp :%+v", string(respByte)))

respByte2, err := r.client.GetTenant(data.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError("F5OS Client Error", fmt.Sprintf("Unable to Read/Get Tenants, got error: %s", err))
stop <- true
resp.Diagnostics.AddError(fmt.Sprintf("%v", err.Error()), "")
return
}
stop <- true
r.tenantResourceModeltoState(ctx, respByte2, data)
tflog.Info(ctx, fmt.Sprintf("Updated State:%+v", data))
// mutex.Unlock()
Expand All @@ -347,10 +366,11 @@ func (r *TenantResource) Delete(ctx context.Context, req resource.DeleteRequest,
if resp.Diagnostics.HasError() {
return
}

stop := r.client.F5OsKeepAlive(15 * time.Second)
err := r.client.DeleteTenant(data.Name.ValueString())
stop <- true
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to Delete Imported Image, got error: %s", err))
resp.Diagnostics.AddError(fmt.Sprintf("%v", err.Error()), "")
return
}
}
Expand Down Expand Up @@ -395,7 +415,7 @@ func (r *TenantResource) tenantResourceModeltoState(ctx context.Context, respDat
data.Cryptos = types.StringValue(respData.F5TenantsTenant[0].State.Cryptos)
}

func getTenantCreateConfig(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) *f5ossdk.F5ReqTenants {
func (r *TenantResource) getTenantCreateConfig(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) *f5ossdk.F5ReqTenants {
var data *TenantResourceModel

// Read Terraform plan data into the model
Expand All @@ -418,11 +438,21 @@ func getTenantCreateConfig(ctx context.Context, req resource.CreateRequest, resp
// tenantSubbj.Config.MacData.F5TenantL2InlineMacBlockSize = data.MacBlockSize.ValueString()
// }
// tenantSubbj.Config.MacData.F5TenantL2InlineMacBlockSize = data.MacBlockSize.ValueString()
if data.Memory.IsNull() {
tenantSubbj.Config.Memory = 3.5*1024*int(data.CpuCores.ValueInt64()) + (512)
} else {
if !data.Memory.IsNull() && !data.Memory.IsUnknown() {
tenantSubbj.Config.Memory = int(data.Memory.ValueInt64())
} else {
tflog.Info(ctx, fmt.Sprintf("r.client.PlatformType:%+v", r.client.PlatformType))
if r.client.PlatformType == "r2800" || r.client.PlatformType == "r2000" || r.client.PlatformType == "r4000" || r.client.PlatformType == "r4800" {
tenantSubbj.Config.Memory = 3 * 1024 * int(data.CpuCores.ValueInt64())
} else {
tenantSubbj.Config.Memory = (3.5 * 1024 * int(data.CpuCores.ValueInt64())) + (512)
}
}

// tenantSubbj.Config.Memory = 3.5*1024*int(data.CpuCores.ValueInt64()) + (512)
// } else {

// }
data.Vlans.ElementsAs(ctx, &tenantSubbj.Config.Vlans, false)
tenantSubbj.Config.PrefixLength = int(data.MgmtPrefix.ValueInt64())
tenantSubbj.Config.RunningState = data.RunningState.ValueString()
Expand All @@ -435,7 +465,7 @@ func getTenantCreateConfig(ctx context.Context, req resource.CreateRequest, resp
return tenantConfig
}

func getTenantUpdateConfig(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) *f5ossdk.F5ReqTenantsPatch {
func (r *TenantResource) getTenantUpdateConfig(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) *f5ossdk.F5ReqTenantsPatch {
var data *TenantResourceModel

// Read Terraform plan data into the model
Expand All @@ -451,13 +481,15 @@ func getTenantUpdateConfig(ctx context.Context, req resource.UpdateRequest, resp
tenantSubbj.Config.VcpuCoresPerNode = int(data.CpuCores.ValueInt64())
tenantSubbj.Config.DagIpv6PrefixLength = int(data.DagIpv6prefixLength.ValueInt64())
tenantSubbj.Config.MacData.F5TenantL2InlineMacBlockSize = data.MacBlockSize.ValueString()
if data.Memory.IsNull() {
tenantSubbj.Config.Memory = 3.5*1024*int(data.CpuCores.ValueInt64()) + (512)
} else {
if !data.Memory.IsNull() && !data.Memory.IsUnknown() {
tenantSubbj.Config.Memory = int(data.Memory.ValueInt64())
} else {
if r.client.PlatformType == "r2800" || r.client.PlatformType == "r2000" || r.client.PlatformType == "r4000" || r.client.PlatformType == "r4800" {
tenantSubbj.Config.Memory = 3 * 1024 * int(data.CpuCores.ValueInt64())
} else {
tenantSubbj.Config.Memory = (3.5 * 1024 * int(data.CpuCores.ValueInt64())) + (512)
}
}
//tenantSubbj.Config.Memory = 3.5*1024*int(data.CpuCores.ValueInt64()) + (512)
//tenantSubbj.Config.Memory = int(data.Memory.ValueInt64())
data.Nodes.ElementsAs(ctx, &tenantSubbj.Config.Nodes, false)
data.Vlans.ElementsAs(ctx, &tenantSubbj.Config.Vlans, false)
tenantSubbj.Config.PrefixLength = int(data.MgmtPrefix.ValueInt64())
Expand Down
55 changes: 51 additions & 4 deletions internal/provider/tenant_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,15 @@ func TestUnitTenantDeployResourceUnitTC1(t *testing.T) {
w.WriteHeader(http.StatusOK)
if r.Method == "GET" && (count == 0 || count == 1 || count == 2) {
_, _ = fmt.Fprintf(w, "%s", loadFixtureString("./fixtures/tenant_config.json"))
}
if r.Method == "GET" && (count == 3 || count == 4 || count == 5) {
} else if r.Method == "GET" && (count == 3 || count == 4 || count == 5) {
_, _ = fmt.Fprintf(w, "%s", loadFixtureString("./fixtures/tenant_update_config.json"))
} else if r.Method == "GET" {
_, _ = fmt.Fprintf(w, `
{"ietf-restconf:errors": {"error": [{
"error-type": "application",
"error-tag": "invalid-value",
"error-message": "uri keypath not found"
}]}}`)
}
count++
})
Expand Down Expand Up @@ -197,6 +203,21 @@ func TestUnitTenantDeployResourceUnitTC2(t *testing.T) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "%s", loadFixtureString("./fixtures/platform_r4k_state.json"))
})
mux.HandleFunc("/restconf/data/f5-tenant-images:images/image=BIGIP-17.1.0-0.0.16.ALL-F5OS.qcow2.zip.bundle", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, `{
"f5-tenant-images:image": [
{
"name": "BIGIP-17.1.0-0.0.16.ALL-F5OS.qcow2.zip.bundle",
"in-use": false,
"type": "vm-image",
"status": "replicated",
"date": "2023-8-17",
"size": "2.27 GB"
}
]
}`)
})
mux.HandleFunc("/restconf/data/f5-tenants:tenants", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
_, _ = fmt.Fprintf(w, ``)
Expand All @@ -205,9 +226,20 @@ func TestUnitTenantDeployResourceUnitTC2(t *testing.T) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "%s", loadFixtureString("./fixtures/tenant_r4k_get_status.json"))
})
var count = 0
mux.HandleFunc("/restconf/data/f5-tenants:tenants/tenant=testtenant-ecosys2", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "%s", loadFixtureString("./fixtures/tenant_r4k_config.json"))
if r.Method == "GET" && (count == 0 || count == 1 || count == 2) {
_, _ = fmt.Fprintf(w, "%s", loadFixtureString("./fixtures/tenant_r4k_config.json"))
} else if r.Method == "GET" {
_, _ = fmt.Fprintf(w, `
{"ietf-restconf:errors": {"error": [{
"error-type": "application",
"error-tag": "invalid-value",
"error-message": "uri keypath not found"
}]}}`)
}
count++
})

defer teardown()
Expand Down Expand Up @@ -250,6 +282,21 @@ func TestUnitTenantDeployResourceUnitTC3(t *testing.T) {
w.WriteHeader(http.StatusNoContent)
_, _ = fmt.Fprintf(w, ``)
})
mux.HandleFunc("/restconf/data/f5-tenant-images:images/image=BIGIP-17.1.0-0.0.16.ALL-F5OS.qcow2.zip.bundle", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, `{
"f5-tenant-images:image": [
{
"name": "BIGIP-17.1.0-0.0.16.ALL-F5OS.qcow2.zip.bundle",
"in-use": false,
"type": "vm-image",
"status": "replicated",
"date": "2023-8-17",
"size": "2.27 GB"
}
]
}`)
})
mux.HandleFunc("/restconf/data/f5-tenants:tenants", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
_, _ = fmt.Fprintf(w, ``)
Expand All @@ -268,7 +315,7 @@ func TestUnitTenantDeployResourceUnitTC3(t *testing.T) {
// Read testing
{
Config: testAccTenantDeployResourceTC3Config,
ExpectError: regexp.MustCompile("Tenant Deploy failed, got error"),
ExpectError: regexp.MustCompile("Tenant Deployment Pending"),
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/vlan_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ func (r *VlanResource) Read(ctx context.Context, req resource.ReadRequest, resp
tflog.Info(ctx, fmt.Sprintf("[READ] Vlan :%+v", data.Id.ValueString()))
vlanId, err := strconv.Atoi(data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("F5OS Client Error", fmt.Sprintf("String to Int conversion failed, got error: %s", err))
resp.Diagnostics.AddError(fmt.Sprintf("[F5OS] %v", err), fmt.Sprintf("String to Int conversion failed for ID:%s", data.Id.ValueString()))
return
}
tflog.Info(ctx, fmt.Sprintf("[READ] Vlan :%+v", vlanId))
partData, err := r.client.GetVlan(vlanId)
if err != nil {
resp.Diagnostics.AddError("F5OS Client Error", fmt.Sprintf("Unable to Read/Get Vlan, got error: %s", err))
resp.Diagnostics.AddError(fmt.Sprintf("%v", err), fmt.Sprintf("Unable to Read/Get Vlan ID:%d", vlanId))
return
}
tflog.Debug(ctx, fmt.Sprintf("VlanResp :%+v", partData))
Expand Down
Loading

0 comments on commit 8988319

Please sign in to comment.