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

Added transaction to resource_bigip_ssl_key_cert #857

Merged
merged 1 commit into from
Aug 31, 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
29 changes: 28 additions & 1 deletion bigip/resource_bigip_ssl_key_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
Passphrase: passphrase,
}

t, err := client.StartTransaction()
if err != nil {
return diag.FromErr(fmt.Errorf("error while starting transaction: %v", err))
}
err = client.AddKey(&keyCfg)
if err != nil {
return diag.FromErr(fmt.Errorf("error while adding the ssl key: %v", err))
Expand All @@ -105,6 +109,10 @@
if err != nil {
return diag.FromErr(fmt.Errorf("error while uploading the ssl cert: %v", err))
}
err = client.CommitTransaction(t.TransID)

Check failure on line 112 in bigip/resource_bigip_ssl_key_cert.go

View workflow job for this annotation

GitHub Actions / golint

client.CommitTransaction undefined (type *"github.com/f5devcentral/go-bigip".BigIP has no field or method CommitTransaction)

Check failure on line 112 in bigip/resource_bigip_ssl_key_cert.go

View workflow job for this annotation

GitHub Actions / golint

client.CommitTransaction undefined (type *"github.com/f5devcentral/go-bigip".BigIP has no field or method CommitTransaction)
if err != nil {
return diag.FromErr(fmt.Errorf("error while ending transaction: %d", err))
}

id := keyName + "_" + certName
d.SetId(id)
Expand Down Expand Up @@ -166,6 +174,11 @@
}

keyFullPath := fmt.Sprintf("/%s/%s", partition, keyName)

t, err := client.StartTransaction()
if err != nil {
return diag.FromErr(fmt.Errorf("error while trying to start transaction: %s", err))
}
err = client.ModifyKey(keyFullPath, &keyCfg)
if err != nil {
return diag.FromErr(fmt.Errorf("error while trying to modify the ssl key (%s): %s", keyFullPath, err))
Expand All @@ -175,6 +188,10 @@
if err != nil {
return diag.FromErr(fmt.Errorf("error while updating the ssl certificate (%s): %s", certName, err))
}
err = client.CommitTransaction(t.TransID)

Check failure on line 191 in bigip/resource_bigip_ssl_key_cert.go

View workflow job for this annotation

GitHub Actions / golint

client.CommitTransaction undefined (type *"github.com/f5devcentral/go-bigip".BigIP has no field or method CommitTransaction)

Check failure on line 191 in bigip/resource_bigip_ssl_key_cert.go

View workflow job for this annotation

GitHub Actions / golint

client.CommitTransaction undefined (type *"github.com/f5devcentral/go-bigip".BigIP has no field or method CommitTransaction)
if err != nil {
return diag.FromErr(fmt.Errorf("error while trying to end transaction: %s", err))
}

return resourceBigipSSLKeyCertRead(ctx, d, meta)
}
Expand All @@ -191,7 +208,12 @@
keyFullPath := "/" + partition + "/" + keyName
certFullPath := "/" + partition + "/" + certName

err := client.DeleteKey(keyFullPath)
t, err := client.StartTransaction()
if err != nil {
return diag.FromErr(fmt.Errorf("error while starting transaction: %v", err))
}

err = client.DeleteKey(keyFullPath)
if err != nil {
log.Printf("[ERROR] unable to delete the ssl key (%s) (%v) ", keyFullPath, err)
}
Expand All @@ -201,6 +223,11 @@
log.Printf("[ERROR] unable to delete the ssl certificate (%s) (%v) ", certFullPath, err)
}

err = client.CommitTransaction(t.TransID)

Check failure on line 226 in bigip/resource_bigip_ssl_key_cert.go

View workflow job for this annotation

GitHub Actions / golint

client.CommitTransaction undefined (type *"github.com/f5devcentral/go-bigip".BigIP has no field or method CommitTransaction)) (typecheck)

Check failure on line 226 in bigip/resource_bigip_ssl_key_cert.go

View workflow job for this annotation

GitHub Actions / golint

client.CommitTransaction undefined (type *"github.com/f5devcentral/go-bigip".BigIP has no field or method CommitTransaction) (typecheck)
if err != nil {
return diag.FromErr(fmt.Errorf("error while ending transaction: %v", err))
}

d.SetId("")
return nil
}
Expand Down
67 changes: 67 additions & 0 deletions bigip/resource_bigip_ssl_key_cert_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package bigip

import (
"fmt"
"log"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -16,6 +19,29 @@ resource "bigip_ssl_key_cert" "testkeycert" {
}
`

var sslProfileCertKey = `
resource "bigip_ssl_key_cert" "testkeycert" {
partition = "Common"
key_name = "ssl-test-key"
key_content = "${file("` + folder + `/../examples/%s")}"
cert_name = "ssl-test-cert"
cert_content = "${file("` + folder + `/../examples/%s")}"
}

resource "bigip_ltm_profile_server_ssl" "test-ServerSsl" {
name = "/Common/test-ServerSsl"
defaults_from = "/Common/serverssl"
authenticate = "always"
ciphers = "DEFAULT"
cert = "/Common/ssl-test-cert"
key = "/Common/ssl-test-key"

depends_on = [
bigip_ssl_key_cert.testkeycert
]
}
`

func TestAccBigipSSLCertKeyCreate(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down Expand Up @@ -45,3 +71,44 @@ func TestAccBigipSSLCertKeyCreate(t *testing.T) {
},
})
}

func TestAccBigipSSLCertKeyCreateCertKeyProfile(t *testing.T) {
create := fmt.Sprintf(sslProfileCertKey, "serverkey.key", "servercert.crt")
modify := fmt.Sprintf(sslProfileCertKey, "serverkey2.key", "servercert2.crt")
crt1Content, _ := os.ReadFile(folder + `/../examples/` + "servercert.crt")
key1Content, _ := os.ReadFile(folder + `/../examples/` + "serverkey.key")
crt2Content, _ := os.ReadFile(folder + `/../examples/` + "servercert2.crt")
key2Content, _ := os.ReadFile(folder + `/../examples/` + "serverkey2.key")

log.Println(create)
log.Println(modify)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAcctPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: create,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "key_name", "ssl-test-key"),
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "cert_name", "ssl-test-cert"),
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "partition", "Common"),
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "key_content", string(key1Content)),
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "cert_content", string(crt1Content)),
),
Destroy: false,
},
{
Config: modify,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "key_name", "ssl-test-key"),
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "cert_name", "ssl-test-cert"),
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "partition", "Common"),
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "key_content", string(key2Content)),
resource.TestCheckResourceAttr("bigip_ssl_key_cert.testkeycert", "cert_content", string(crt2Content)),
),
},
},
})
}
16 changes: 7 additions & 9 deletions vendor/github.com/f5devcentral/go-bigip/sys.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading