netboxgo/secrets.go

102 lines
2.6 KiB
Go
Raw Normal View History

2019-12-17 09:29:48 +01:00
package netboxgo
import (
2021-11-26 11:09:27 +01:00
"context"
"fmt"
2019-12-17 09:29:48 +01:00
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
2021-11-26 11:09:27 +01:00
type SecretsService service
// Secrets contains secrets
type Secrets struct {
2019-12-17 09:29:48 +01:00
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"`
2021-11-26 11:09:27 +01:00
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
2019-12-17 09:29:48 +01:00
} `json:"results"`
}
2020-03-02 10:00:40 +01:00
// SecretFilter is used to filter out secrets
2019-12-17 09:29:48 +01:00
type SecretFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
2019-12-17 09:29:48 +01:00
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"
2019-12-17 09:29:48 +01:00
2021-11-26 11:09:27 +01:00
// 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
2019-12-17 09:29:48 +01:00
2021-11-26 11:09:27 +01:00
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()")
2019-12-17 09:29:48 +01:00
}
2021-11-26 11:09:27 +01:00
encoder := schema.NewEncoder()
2019-12-17 09:29:48 +01:00
2021-11-26 11:09:27 +01:00
form := url.Values{}
err = encoder.Encode(f, form)
2019-12-17 09:29:48 +01:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &secrets, err
2019-12-17 09:29:48 +01:00
}
2021-11-26 11:09:27 +01:00
query = form.Encode()
2019-12-17 09:29:48 +01:00
req, err = s.client.newRequest(ctx, "GET", secretsPath+"/secrets", query, nil)
2019-12-17 09:29:48 +01:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &secrets, err
2019-12-17 09:29:48 +01:00
}
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, &secrets)
2019-12-17 09:29:48 +01:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &secrets, err
2019-12-17 09:29:48 +01:00
}
2021-11-26 11:09:27 +01:00
return &secrets, nil
2019-12-17 09:29:48 +01:00
}