netboxgo/secrets.go
2021-11-26 11:15:19 +01:00

104 lines
2.6 KiB
Go

package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type SecretsService service
// Secrets contains secrets
type Secrets struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ID int `json:"id"`
Device struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
} `json:"device"`
Role struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
SecretCount int `json:"secret_count"`
} `json:"role"`
Name string `json:"name"`
Plaintext string `json:"plaintext"`
Hash string `json:"hash"`
Tags []struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
} `json:"results"`
}
// SecretFilter is used to filter out secrets
type SecretFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
Name string `schema:"name,omitempty"`
Role string `schema:"role,omitempty"`
RoleID string `schema:"role_id,omitempty"`
DeviceID string `schema:"device_id,omitempty"`
TypeID string `schema:"type_id,omitempty"`
Device string `schema:"device,omitempty"`
IDIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
Tag string `schema:"tag,omitempty"`
}
const secretsPath = "/secrets/secrets"
// List secrets. SecretsFilter is used to list based on filter queries.
func (s *SecretsService) List(ctx context.Context, f *SecretFilter) (*Secrets, error) {
var secrets Secrets
var query string
var req *http.Request
var err error
if s.client.SessionKey == "" {
return &secrets, fmt.Errorf("session key needed when interacting with secrets. no session key found: you need to fetch a session key with FetchSessionKey()")
}
encoder := schema.NewEncoder()
form := url.Values{}
err = encoder.Encode(f, form)
if err != nil {
return &secrets, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", secretsPath, query, nil)
if err != nil {
return &secrets, err
}
_, err = s.client.do(req, &secrets)
if err != nil {
return &secrets, err
}
// Clean c.client.SessionKey from memory
s.client.SessionKey = ""
return &secrets, nil
}