|
| 1 | +package tsigkeys |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gophercloud/gophercloud/v2" |
| 7 | + "github.com/gophercloud/gophercloud/v2/pagination" |
| 8 | +) |
| 9 | + |
| 10 | +// ListOptsBuilder allows extensions to add parameters to the List request. |
| 11 | +type ListOptsBuilder interface { |
| 12 | + ToTSIGKeyListQuery() (string, error) |
| 13 | +} |
| 14 | + |
| 15 | +// ListOpts allows the filtering and sorting of paginated collections through |
| 16 | +// the API. Filtering is achieved by passing in struct field values that map to |
| 17 | +// the server attributes you want to see returned. Marker and Limit are used |
| 18 | +// for pagination. |
| 19 | +// https://docs.openstack.org/api-ref/dns/ |
| 20 | +type ListOpts struct { |
| 21 | + // Integer value for the limit of values to return. |
| 22 | + Limit int `q:"limit"` |
| 23 | + |
| 24 | + // UUID of the TSIG key at which you want to set a marker. |
| 25 | + Marker string `q:"marker"` |
| 26 | + |
| 27 | + // Name of the TSIG key. |
| 28 | + Name string `q:"name"` |
| 29 | + |
| 30 | + // Algorithm used by the TSIG key. |
| 31 | + Algorithm string `q:"algorithm"` |
| 32 | + |
| 33 | + // Scope of the TSIG key (ZONE or POOL). |
| 34 | + Scope string `q:"scope"` |
| 35 | +} |
| 36 | + |
| 37 | +// ToTSIGKeyListQuery formats a ListOpts into a query string. |
| 38 | +func (opts ListOpts) ToTSIGKeyListQuery() (string, error) { |
| 39 | + q, err := gophercloud.BuildQueryString(opts) |
| 40 | + return q.String(), err |
| 41 | +} |
| 42 | + |
| 43 | +// List implements a TSIG key List request. |
| 44 | +func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 45 | + url := baseURL(client) |
| 46 | + if opts != nil { |
| 47 | + query, err := opts.ToTSIGKeyListQuery() |
| 48 | + if err != nil { |
| 49 | + return pagination.Pager{Err: err} |
| 50 | + } |
| 51 | + url += query |
| 52 | + } |
| 53 | + return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 54 | + return TSIGKeyPage{pagination.LinkedPageBase{PageResult: r}} |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +// Get returns information about a TSIG key, given its ID. |
| 59 | +func Get(ctx context.Context, client *gophercloud.ServiceClient, tsigkeyID string) (r GetResult) { |
| 60 | + resp, err := client.Get(ctx, tsigkeyURL(client, tsigkeyID), &r.Body, nil) |
| 61 | + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) |
| 62 | + return |
| 63 | +} |
| 64 | + |
| 65 | +// CreateOptsBuilder allows extensions to add additional attributes to the |
| 66 | +// Create request. |
| 67 | +type CreateOptsBuilder interface { |
| 68 | + ToTSIGKeyCreateMap() (map[string]any, error) |
| 69 | +} |
| 70 | + |
| 71 | +// CreateOpts specifies the attributes used to create a TSIG key. |
| 72 | +type CreateOpts struct { |
| 73 | + // Name of the TSIG key. |
| 74 | + Name string `json:"name" required:"true"` |
| 75 | + |
| 76 | + // Algorithm is the TSIG algorithm (e.g., hmac-sha256, hmac-sha512). |
| 77 | + Algorithm string `json:"algorithm" required:"true"` |
| 78 | + |
| 79 | + // Secret is the base64-encoded secret key. |
| 80 | + Secret string `json:"secret" required:"true"` |
| 81 | + |
| 82 | + // Scope defines the scope of the TSIG key (ZONE or POOL). |
| 83 | + Scope string `json:"scope" required:"true"` |
| 84 | + |
| 85 | + // ResourceID is the ID of the resource (zone or pool) this key is associated with. |
| 86 | + ResourceID string `json:"resource_id" required:"true"` |
| 87 | +} |
| 88 | + |
| 89 | +// ToTSIGKeyCreateMap formats a CreateOpts structure into a request body. |
| 90 | +func (opts CreateOpts) ToTSIGKeyCreateMap() (map[string]any, error) { |
| 91 | + return gophercloud.BuildRequestBody(opts, "") |
| 92 | +} |
| 93 | + |
| 94 | +// Create implements a TSIG key create request. |
| 95 | +func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 96 | + b, err := opts.ToTSIGKeyCreateMap() |
| 97 | + if err != nil { |
| 98 | + r.Err = err |
| 99 | + return |
| 100 | + } |
| 101 | + resp, err := client.Post(ctx, baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{ |
| 102 | + OkCodes: []int{201}, |
| 103 | + }) |
| 104 | + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) |
| 105 | + return |
| 106 | +} |
| 107 | + |
| 108 | +// UpdateOptsBuilder allows extensions to add additional attributes to the |
| 109 | +// Update request. |
| 110 | +type UpdateOptsBuilder interface { |
| 111 | + ToTSIGKeyUpdateMap() (map[string]any, error) |
| 112 | +} |
| 113 | + |
| 114 | +// UpdateOpts specifies the attributes to update a TSIG key. |
| 115 | +type UpdateOpts struct { |
| 116 | + // Name of the TSIG key. |
| 117 | + Name string `json:"name,omitempty"` |
| 118 | + |
| 119 | + // Algorithm is the TSIG algorithm. |
| 120 | + Algorithm string `json:"algorithm,omitempty"` |
| 121 | + |
| 122 | + // Secret is the base64-encoded secret key. |
| 123 | + Secret string `json:"secret,omitempty"` |
| 124 | + |
| 125 | + // Scope defines the scope of the TSIG key. |
| 126 | + Scope string `json:"scope,omitempty"` |
| 127 | + |
| 128 | + // ResourceID is the ID of the resource this key is associated with. |
| 129 | + ResourceID string `json:"resource_id,omitempty"` |
| 130 | +} |
| 131 | + |
| 132 | +// ToTSIGKeyUpdateMap formats an UpdateOpts structure into a request body. |
| 133 | +func (opts UpdateOpts) ToTSIGKeyUpdateMap() (map[string]any, error) { |
| 134 | + return gophercloud.BuildRequestBody(opts, "") |
| 135 | +} |
| 136 | + |
| 137 | +// Update implements a TSIG key update request. |
| 138 | +func Update(ctx context.Context, client *gophercloud.ServiceClient, tsigkeyID string, opts UpdateOptsBuilder) (r UpdateResult) { |
| 139 | + b, err := opts.ToTSIGKeyUpdateMap() |
| 140 | + if err != nil { |
| 141 | + r.Err = err |
| 142 | + return |
| 143 | + } |
| 144 | + resp, err := client.Patch(ctx, tsigkeyURL(client, tsigkeyID), &b, &r.Body, &gophercloud.RequestOpts{ |
| 145 | + OkCodes: []int{200}, |
| 146 | + }) |
| 147 | + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) |
| 148 | + return |
| 149 | +} |
| 150 | + |
| 151 | +// Delete implements a TSIG key delete request. |
| 152 | +func Delete(ctx context.Context, client *gophercloud.ServiceClient, tsigkeyID string) (r DeleteResult) { |
| 153 | + resp, err := client.Delete(ctx, tsigkeyURL(client, tsigkeyID), &gophercloud.RequestOpts{ |
| 154 | + OkCodes: []int{204}, |
| 155 | + }) |
| 156 | + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) |
| 157 | + return |
| 158 | +} |
0 commit comments