Skip to content

Commit

Permalink
Merge pull request #97 from jqueuniet/context
Browse files Browse the repository at this point in the history
Add context support
  • Loading branch information
luthermonson committed Oct 25, 2023
2 parents 413ae35 + b660812 commit 6c8706e
Show file tree
Hide file tree
Showing 22 changed files with 452 additions and 384 deletions.
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -38,7 +38,9 @@ Create a client and use the public methods to access Proxmox resources.
package main

import (
"context"
"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
@@ -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{
Username: username,
Password: password,
})
Expand All @@ -21,20 +22,20 @@ func (c *Client) APIToken(tokenID, secret string) {
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)
}

// 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 @@ func (c *Client) Permissions(o *PermissionsOptions) (permissions Permissions, er
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 @@ func (c *Client) Domains() (domains Domains, err error) {
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 @@ func (c *Client) Groups() (groups Groups, err error) {
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 @@ func (c *Client) Users() (users Users, err error) {
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)
}

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)
}

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 @@ func (c *Client) Roles() (roles Roles, err error) {
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)
}

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)
}

0 comments on commit 6c8706e

Please sign in to comment.