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

Add context support #97

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ Create a client and use the public methods to access Proxmox resources.
package main

import (
"context"
luthermonson marked this conversation as resolved.
Show resolved Hide resolved
"fmt"

"github.com/luthermonson/go-proxmox"
)

Expand All @@ -51,7 +53,7 @@ func main() {
proxmox.WithCredentials(&credentials),
)

version, err := client.Version()
version, err := client.Version(context.Background())
if err != nil {
panic(err)
}
Expand All @@ -64,7 +66,11 @@ func main() {
package main

import (
"context"
"crypto/tls"
"fmt"
"net/http"

"github.com/luthermonson/go-proxmox"
)

Expand All @@ -84,7 +90,7 @@ func main() {
proxmox.WithAPIToken(tokenID, secret),
)

version, err := client.Version()
version, err := client.Version(context.Background())
if err != nil {
panic(err)
}
Expand Down
101 changes: 51 additions & 50 deletions access.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package proxmox

import (
"context"
"errors"
"fmt"
"net/url"
)

// Deprecated: Use WithCredentials Option
func (c *Client) Login(username, password string) error {
_, err := c.Ticket(&Credentials{
func (c *Client) Login(ctx context.Context, username, password string) error {
_, err := c.Ticket(ctx, &Credentials{

Check warning on line 12 in access.go

View check run for this annotation

Codecov / codecov/patch

access.go#L11-L12

Added lines #L11 - L12 were not covered by tests
Username: username,
Password: password,
})
Expand All @@ -21,20 +22,20 @@
c.token = fmt.Sprintf("%s=%s", tokenID, secret)
}

func (c *Client) Ticket(credentials *Credentials) (*Session, error) {
return c.session, c.Post("/access/ticket", credentials, &c.session)
func (c *Client) Ticket(ctx context.Context, credentials *Credentials) (*Session, error) {
return c.session, c.Post(ctx, "/access/ticket", credentials, &c.session)
}

func (c *Client) ACL() (acl ACLs, err error) {
return acl, c.Get("/access/acl", &acl)
func (c *Client) ACL(ctx context.Context) (acl ACLs, err error) {
return acl, c.Get(ctx, "/access/acl", &acl)
}

func (c *Client) UpdateACL(acl ACL) error {
return c.Put("/access/acl", &acl, nil)
func (c *Client) UpdateACL(ctx context.Context, acl ACL) error {
return c.Put(ctx, "/access/acl", &acl, nil)

Check warning on line 34 in access.go

View check run for this annotation

Codecov / codecov/patch

access.go#L33-L34

Added lines #L33 - L34 were not covered by tests
}

// Permissions get permissions for the current user for the client which passes no params, use Permission
func (c *Client) Permissions(o *PermissionsOptions) (permissions Permissions, err error) {
func (c *Client) Permissions(ctx context.Context, o *PermissionsOptions) (permissions Permissions, err error) {
u := url.URL{Path: "/access/permissions"}

if o != nil { // params are optional
Expand All @@ -48,35 +49,35 @@
u.RawQuery = params.Encode()
}

return permissions, c.Get(u.String(), &permissions)
return permissions, c.Get(ctx, u.String(), &permissions)
}

func (c *Client) Password(userid, password string) error {
return c.Post("/access/password", map[string]string{
func (c *Client) Password(ctx context.Context, userid, password string) error {
return c.Post(ctx, "/access/password", map[string]string{
"userid": userid,
"password": password,
}, nil)
}

// NewDomain create a new domain with the required two parameters pull it and use domain.Update to configure
func (c *Client) NewDomain(realm string, domainType DomainType) error {
return c.Post("/access/domains", map[string]string{
func (c *Client) NewDomain(ctx context.Context, realm string, domainType DomainType) error {
return c.Post(ctx, "/access/domains", map[string]string{
"realm": realm,
"type": string(domainType),
}, nil)
}

func (c *Client) Domain(realm string) (domain *Domain, err error) {
err = c.Get(fmt.Sprintf("/access/domains/%s", realm), &domain)
func (c *Client) Domain(ctx context.Context, realm string) (domain *Domain, err error) {
err = c.Get(ctx, fmt.Sprintf("/access/domains/%s", realm), &domain)
if nil == err {
domain.Realm = realm
domain.client = c
}
return
}

func (c *Client) Domains() (domains Domains, err error) {
err = c.Get("/access/domains", &domains)
func (c *Client) Domains(ctx context.Context) (domains Domains, err error) {
err = c.Get(ctx, "/access/domains", &domains)
if nil == err {
for _, d := range domains {
d.client = c
Expand All @@ -85,46 +86,46 @@
return
}

func (d *Domain) Update() error {
func (d *Domain) Update(ctx context.Context) error {
if d.Realm == "" {
return errors.New("realm can not be empty")
}
return d.client.Put(fmt.Sprintf("/access/domains/%s", d.Realm), d, nil)
return d.client.Put(ctx, fmt.Sprintf("/access/domains/%s", d.Realm), d, nil)
}

func (d *Domain) Delete() error {
func (d *Domain) Delete(ctx context.Context) error {
if d.Realm == "" {
return errors.New("realm can not be empty")
}
return d.client.Delete(fmt.Sprintf("/access/domains/%s", d.Realm), nil)
return d.client.Delete(ctx, fmt.Sprintf("/access/domains/%s", d.Realm), nil)
}

func (d *Domain) Sync(options DomainSyncOptions) error {
func (d *Domain) Sync(ctx context.Context, options DomainSyncOptions) error {
if d.Realm == "" {
return errors.New("realm can not be empty")
}
return d.client.Post(fmt.Sprintf("/access/domains/%s", d.Realm), options, nil)
return d.client.Post(ctx, fmt.Sprintf("/access/domains/%s", d.Realm), options, nil)
}

// NewGroup makes a new group, comment is option and can be left empty
func (c *Client) NewGroup(groupid, comment string) error {
return c.Post("/access/groups", map[string]string{
func (c *Client) NewGroup(ctx context.Context, groupid, comment string) error {
return c.Post(ctx, "/access/groups", map[string]string{
"groupid": groupid,
"comment": comment,
}, nil)
}

func (c *Client) Group(groupid string) (group *Group, err error) {
err = c.Get(fmt.Sprintf("/access/groups/%s", groupid), &group)
func (c *Client) Group(ctx context.Context, groupid string) (group *Group, err error) {
err = c.Get(ctx, fmt.Sprintf("/access/groups/%s", groupid), &group)
if nil == err {
group.GroupID = groupid
group.client = c
}
return
}

func (c *Client) Groups() (groups Groups, err error) {
err = c.Get("/access/groups", &groups)
func (c *Client) Groups(ctx context.Context) (groups Groups, err error) {
err = c.Get(ctx, "/access/groups", &groups)
if nil == err {
for _, g := range groups {
g.client = c
Expand All @@ -133,25 +134,25 @@
return
}

func (g *Group) Update() error {
return g.client.Put(fmt.Sprintf("/access/groups/%s", g.GroupID), g, nil)
func (g *Group) Update(ctx context.Context) error {
return g.client.Put(ctx, fmt.Sprintf("/access/groups/%s", g.GroupID), g, nil)
}

func (g *Group) Delete() error {
return g.client.Delete(fmt.Sprintf("/access/groups/%s", g.GroupID), nil)
func (g *Group) Delete(ctx context.Context) error {
return g.client.Delete(ctx, fmt.Sprintf("/access/groups/%s", g.GroupID), nil)
}

func (c *Client) User(userid string) (user *User, err error) {
err = c.Get(fmt.Sprintf("/access/users/%s", userid), &user)
func (c *Client) User(ctx context.Context, userid string) (user *User, err error) {
err = c.Get(ctx, fmt.Sprintf("/access/users/%s", userid), &user)
if nil == err {
user.UserID = userid
user.client = c
}
return
}

func (c *Client) Users() (users Users, err error) {
err = c.Get("/access/users", &users)
func (c *Client) Users(ctx context.Context) (users Users, err error) {
err = c.Get(ctx, "/access/users", &users)
if nil == err {
for _, g := range users {
g.client = c
Expand All @@ -160,21 +161,21 @@
return
}

func (u *User) Update() error {
return u.client.Put(fmt.Sprintf("/access/users/%s", u.UserID), u, nil)
func (u *User) Update(ctx context.Context) error {
return u.client.Put(ctx, fmt.Sprintf("/access/users/%s", u.UserID), u, nil)

Check warning on line 165 in access.go

View check run for this annotation

Codecov / codecov/patch

access.go#L164-L165

Added lines #L164 - L165 were not covered by tests
}

func (u *User) Delete() error {
return u.client.Delete(fmt.Sprintf("/access/users/%s", u.UserID), nil)
func (u *User) Delete(ctx context.Context) error {
return u.client.Delete(ctx, fmt.Sprintf("/access/users/%s", u.UserID), nil)

Check warning on line 169 in access.go

View check run for this annotation

Codecov / codecov/patch

access.go#L168-L169

Added lines #L168 - L169 were not covered by tests
}

func (c *Client) Role(roleid string) (role Permission, err error) {
err = c.Get(fmt.Sprintf("/access/roles/%s", roleid), &role)
func (c *Client) Role(ctx context.Context, roleid string) (role Permission, err error) {
err = c.Get(ctx, fmt.Sprintf("/access/roles/%s", roleid), &role)
return
}

func (c *Client) Roles() (roles Roles, err error) {
err = c.Get("/access/roles", &roles)
func (c *Client) Roles(ctx context.Context) (roles Roles, err error) {
err = c.Get(ctx, "/access/roles", &roles)
if nil == err {
for _, g := range roles {
g.client = c
Expand All @@ -183,10 +184,10 @@
return
}

func (r *Role) Update() error {
return r.client.Put(fmt.Sprintf("/access/roles/%s", r.RoleID), r, nil)
func (r *Role) Update(ctx context.Context) error {
return r.client.Put(ctx, fmt.Sprintf("/access/roles/%s", r.RoleID), r, nil)

Check warning on line 188 in access.go

View check run for this annotation

Codecov / codecov/patch

access.go#L187-L188

Added lines #L187 - L188 were not covered by tests
}

func (r *Role) Delete() error {
return r.client.Delete(fmt.Sprintf("/access/roles/%s", r.RoleID), nil)
func (r *Role) Delete(ctx context.Context) error {
return r.client.Delete(ctx, fmt.Sprintf("/access/roles/%s", r.RoleID), nil)

Check warning on line 192 in access.go

View check run for this annotation

Codecov / codecov/patch

access.go#L191-L192

Added lines #L191 - L192 were not covered by tests
}
Loading
Loading