Skip to content

Commit

Permalink
feat: add context (#1197)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon authored Dec 7, 2024
1 parent fcec4bd commit 7e66709
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 167 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.0

require (
github.com/Telmate/proxmox-api-go v0.0.0-20241121215203-222002bcb5bc
github.com/Telmate/proxmox-api-go v0.0.0-20241205214358-976ef5098918
github.com/google/uuid v1.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/ProtonMail/go-crypto v1.1.0-alpha.2-proton h1:HKz85FwoXx86kVtTvFke7rgHvq/HoloSUvW5semjFWs=
github.com/ProtonMail/go-crypto v1.1.0-alpha.2-proton/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
github.com/Telmate/proxmox-api-go v0.0.0-20241121215203-222002bcb5bc h1:gKm4H2rehb9iTWh4OW/Yt/SNJwoTLHTam+Trjkz2HHY=
github.com/Telmate/proxmox-api-go v0.0.0-20241121215203-222002bcb5bc/go.mod h1:Gu6n6vEn1hlyFUkjrvU+X1fdgaSXLoM9HKYYJqy1fsY=
github.com/Telmate/proxmox-api-go v0.0.0-20241205214358-976ef5098918 h1:dfs2cVaIHtia0uq/Vo6y8pF0McWadzFPW7mAB46d1JI=
github.com/Telmate/proxmox-api-go v0.0.0-20241205214358-976ef5098918/go.mod h1:Gu6n6vEn1hlyFUkjrvU+X1fdgaSXLoM9HKYYJqy1fsY=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
Expand Down
13 changes: 7 additions & 6 deletions proxmox/data_ha_group.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package proxmox

import (
"context"
"sort"

"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataHAGroup() *schema.Resource {
return &schema.Resource{
Read: dataReadHAGroup,
ReadContext: dataReadHAGroup,
Schema: map[string]*schema.Schema{
"group_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -42,17 +43,17 @@ func DataHAGroup() *schema.Resource {
}
}

func dataReadHAGroup(data *schema.ResourceData, meta interface{}) (err error) {
func dataReadHAGroup(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {

pconf := meta.(*providerConfiguration)
lock := pmParallelBegin(pconf)
defer lock.unlock()

client := pconf.Client

var group *proxmox.HAGroup
group, err = client.GetHAGroupByName(data.Get("group_name").(string))
group, err := client.GetHAGroupByName(ctx, data.Get("group_name").(string))
if err != nil {
return err
return diag.FromErr(err)
}

nodes := group.Nodes
Expand Down
8 changes: 5 additions & 3 deletions proxmox/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proxmox

import (
"context"
"crypto/tls"
"fmt"
"net/url"
Expand Down Expand Up @@ -264,7 +265,8 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
if err != nil {
return nil, err
}
permlist, err := client.GetUserPermissions(userID, "/")
ctx := context.Background()
permlist, err := client.GetUserPermissions(ctx, userID, "/")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -348,7 +350,7 @@ func getClient(pm_api_url string,

// User+Pass authentication
if pm_user != "" && pm_password != "" {
err = client.Login(pm_user, pm_password, pm_otp)
err = client.Login(context.Background(), pm_user, pm_password, pm_otp)
}

// API authentication
Expand All @@ -366,7 +368,7 @@ func getClient(pm_api_url string,
func nextVmId(pconf *providerConfiguration) (nextId int, err error) {
pconf.Mutex.Lock()
defer pconf.Mutex.Unlock()
nextId, err = pconf.Client.GetNextID(0)
nextId, err = pconf.Client.GetNextID(context.Background(), 0)
if err != nil {
return 0, err
}
Expand Down
6 changes: 3 additions & 3 deletions proxmox/resource_cloud_init_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func resourceCloudInitDiskCreate(ctx context.Context, d *schema.ResourceData, m
}

fileName := fmt.Sprintf("tf-ci-%s.iso", d.Get("name").(string))
err = client.Upload(d.Get("pve_node").(string), d.Get("storage").(string), isoContentType, fileName, r)
err = client.Upload(ctx, d.Get("pve_node").(string), d.Get("storage").(string), isoContentType, fileName, r)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -154,7 +154,7 @@ func resourceCloudInitDiskRead(ctx context.Context, d *schema.ResourceData, m in
vmRef := &proxmox.VmRef{}
vmRef.SetNode(pveNode)
vmRef.SetVmType("qemu")
storageContent, err := client.GetStorageContent(vmRef, d.Get("storage").(string))
storageContent, err := client.GetStorageContent(ctx, vmRef, d.Get("storage").(string))
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func resourceCloudInitDiskDelete(ctx context.Context, d *schema.ResourceData, m

storage := strings.SplitN(d.Id(), ":", 2)[0]
isoURL := fmt.Sprintf("/nodes/%s/storage/%s/content/%s", d.Get("pve_node").(string), storage, d.Id())
err := client.Delete(isoURL)
err := client.Delete(ctx, isoURL)
if err != nil {
return diag.FromErr(err)
}
Expand Down
Loading

0 comments on commit 7e66709

Please sign in to comment.