Skip to content

Commit ad8066c

Browse files
authored
Subscriptions Vendor Passthru support (#2201)
This commit adds support for the following vendor passthru methods: - create_subscription - delete_subscription - get_subscription - get_all_subscriptions This methods are only supported by redfish implementations in ironic that are using the vendor passthru.
1 parent 1e1b375 commit ad8066c

File tree

6 files changed

+652
-0
lines changed

6 files changed

+652
-0
lines changed

openstack/baremetal/v1/nodes/doc.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,67 @@ Example to get boot device for a node
126126
if err != nil {
127127
panic(err)
128128
}
129+
130+
Example to list all vendor passthru methods
131+
132+
methods, err := nodes.GetVendorPassthruMethods(client, "a62b8495-52e2-407b-b3cb-62775d04c2b8").Extract()
133+
if err != nil {
134+
panic(err)
135+
}
136+
137+
Example to list all subscriptions
138+
139+
method := nodes.CallVendorPassthruOpts{
140+
Method: "get_all_subscriptions",
141+
}
142+
allSubscriptions, err := nodes.GetAllSubscriptions(client, "a62b8495-52e2-407b-b3cb-62775d04c2b8", method).Extract()
143+
if err != nil {
144+
panic(err)
145+
}
146+
147+
Example to get a subscription
148+
149+
method := nodes.CallVendorPassthruOpts{
150+
Method: "get_subscription",
151+
}
152+
subscriptionOpt := nodes.GetSubscriptionOpts{
153+
Id: "subscription id",
154+
}
155+
156+
subscription, err := nodes.GetSubscription(client, "a62b8495-52e2-407b-b3cb-62775d04c2b8", method, subscriptionOpt).Extract()
157+
if err != nil {
158+
panic(err)
159+
}
160+
161+
Example to delete a subscription
162+
163+
method := nodes.CallVendorPassthruOpts{
164+
Method: "delete_subscription",
165+
}
166+
subscriptionDeleteOpt := nodes.DeleteSubscriptionOpts{
167+
Id: "subscription id",
168+
}
169+
170+
err := nodes.DeleteSubscription(client, "a62b8495-52e2-407b-b3cb-62775d04c2b8", method, subscriptionDeleteOpt).ExtractErr()
171+
if err != nil {
172+
panic(err)
173+
}
174+
175+
Example to create a subscription
176+
177+
method := nodes.CallVendorPassthruOpts{
178+
Method: "create_subscription",
179+
}
180+
subscriptionCreateOpt := nodes.CreateSubscriptionOpts{
181+
Destination: "https://subscription_destination_url"
182+
Context: "MyContext",
183+
Protocol: "Redfish",
184+
EventTypes: ["Alert"],
185+
}
186+
187+
newSubscription, err := nodes.CreateSubscription(client, "a62b8495-52e2-407b-b3cb-62775d04c2b8", method, subscriptionCreateOpt).Extract()
188+
if err != nil {
189+
panic(err)
190+
}
129191
*/
130192
package nodes

openstack/baremetal/v1/nodes/requests.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,3 +694,143 @@ func GetBIOSSetting(client *gophercloud.ServiceClient, id string, setting string
694694
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
695695
return
696696
}
697+
698+
// CallVendorPassthruOpts defines query options that can be passed to any VendorPassthruCall
699+
type CallVendorPassthruOpts struct {
700+
Method string `q:"method"`
701+
}
702+
703+
// ToGetSubscriptionMap assembles a query based on the contents of a CallVendorPassthruOpts
704+
func ToGetAllSubscriptionMap(opts CallVendorPassthruOpts) (string, error) {
705+
q, err := gophercloud.BuildQueryString(opts)
706+
return q.String(), err
707+
}
708+
709+
// Get all vendor_passthru methods available for the given Node.
710+
func GetVendorPassthruMethods(client *gophercloud.ServiceClient, id string) (r VendorPassthruMethodsResult) {
711+
resp, err := client.Get(vendorPassthruMethodsURL(client, id), &r.Body, &gophercloud.RequestOpts{
712+
OkCodes: []int{200},
713+
})
714+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
715+
return
716+
}
717+
718+
// Get all subscriptions available for the given Node.
719+
func GetAllSubscriptions(client *gophercloud.ServiceClient, id string, method CallVendorPassthruOpts) (r GetAllSubscriptionsVendorPassthruResult) {
720+
query, err := ToGetAllSubscriptionMap(method)
721+
if err != nil {
722+
r.Err = err
723+
return
724+
}
725+
url := vendorPassthruCallURL(client, id) + query
726+
resp, err := client.Get(url, &r.Body, &gophercloud.RequestOpts{
727+
OkCodes: []int{200},
728+
})
729+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
730+
return
731+
}
732+
733+
// The desired subscription id on the baremetal node.
734+
type GetSubscriptionOpts struct {
735+
Id string `json:"id"`
736+
}
737+
738+
// ToGetSubscriptionMap assembles a query based on the contents of CallVendorPassthruOpts and a request body based on the contents of a GetSubscriptionOpts
739+
func ToGetSubscriptionMap(method CallVendorPassthruOpts, opts GetSubscriptionOpts) (string, map[string]interface{}, error) {
740+
q, err := gophercloud.BuildQueryString(method)
741+
if err != nil {
742+
return q.String(), nil, err
743+
}
744+
body, err := gophercloud.BuildRequestBody(opts, "")
745+
if err != nil {
746+
return q.String(), nil, err
747+
}
748+
749+
return q.String(), body, nil
750+
}
751+
752+
// Get a subscription on the given Node.
753+
func GetSubscription(client *gophercloud.ServiceClient, id string, method CallVendorPassthruOpts, subscriptionOpts GetSubscriptionOpts) (r SubscriptionVendorPassthruResult) {
754+
query, reqBody, err := ToGetSubscriptionMap(method, subscriptionOpts)
755+
if err != nil {
756+
r.Err = err
757+
return
758+
}
759+
url := vendorPassthruCallURL(client, id) + query
760+
resp, err := client.Get(url, &r.Body, &gophercloud.RequestOpts{
761+
JSONBody: reqBody,
762+
OkCodes: []int{200},
763+
})
764+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
765+
return
766+
}
767+
768+
// The desired subscription to be deleted from the baremetal node.
769+
type DeleteSubscriptionOpts struct {
770+
Id string `json:"id"`
771+
}
772+
773+
// ToDeleteSubscriptionMap assembles a query based on the contents of CallVendorPassthruOpts and a request body based on the contents of a DeleteSubscriptionOpts
774+
func ToDeleteSubscriptionMap(method CallVendorPassthruOpts, opts DeleteSubscriptionOpts) (string, map[string]interface{}, error) {
775+
q, err := gophercloud.BuildQueryString(method)
776+
if err != nil {
777+
return q.String(), nil, err
778+
}
779+
body, err := gophercloud.BuildRequestBody(opts, "")
780+
if err != nil {
781+
return q.String(), nil, err
782+
}
783+
return q.String(), body, nil
784+
}
785+
786+
// Delete a subscription on the given node.
787+
func DeleteSubscription(client *gophercloud.ServiceClient, id string, method CallVendorPassthruOpts, subscriptionOpts DeleteSubscriptionOpts) (r DeleteSubscriptionVendorPassthruResult) {
788+
query, reqBody, err := ToDeleteSubscriptionMap(method, subscriptionOpts)
789+
if err != nil {
790+
r.Err = err
791+
return
792+
}
793+
url := vendorPassthruCallURL(client, id) + query
794+
resp, err := client.Delete(url, &gophercloud.RequestOpts{
795+
JSONBody: reqBody,
796+
OkCodes: []int{200, 202, 204},
797+
})
798+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
799+
return r
800+
}
801+
802+
// The desired subscription to be created from the baremetal node.
803+
type CreateSubscriptionOpts struct {
804+
Destination string `json:"Destination"`
805+
EventTypes []string `json:"EventTypes,omitempty"`
806+
Context string `json:"Context,omitempty"`
807+
Protocol string `json:"Protocol,omitempty"`
808+
}
809+
810+
// ToCreateSubscriptionMap assembles a query based on the contents of CallVendorPassthruOpts and a request body based on the contents of a CreateSubscriptionOpts
811+
func ToCreateSubscriptionMap(method CallVendorPassthruOpts, opts CreateSubscriptionOpts) (string, map[string]interface{}, error) {
812+
q, err := gophercloud.BuildQueryString(method)
813+
if err != nil {
814+
return q.String(), nil, err
815+
}
816+
body, err := gophercloud.BuildRequestBody(opts, "")
817+
if err != nil {
818+
return q.String(), nil, err
819+
}
820+
return q.String(), body, nil
821+
}
822+
823+
// Creates a subscription on the given node.
824+
func CreateSubscription(client *gophercloud.ServiceClient, id string, method CallVendorPassthruOpts, subscriptionOpts CreateSubscriptionOpts) (r SubscriptionVendorPassthruResult) {
825+
query, reqBody, err := ToCreateSubscriptionMap(method, subscriptionOpts)
826+
if err != nil {
827+
r.Err = err
828+
return
829+
}
830+
url := vendorPassthruCallURL(client, id) + query
831+
resp, err := client.Post(url, reqBody, &r.Body, &gophercloud.RequestOpts{
832+
OkCodes: []int{200},
833+
})
834+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
835+
return r
836+
}

openstack/baremetal/v1/nodes/results.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ func (r GetBIOSSettingResult) Extract() (*BIOSSetting, error) {
6565
return &s.Setting, err
6666
}
6767

68+
// Extract interprets a VendorPassthruMethod as
69+
func (r VendorPassthruMethodsResult) Extract() (*VendorPassthruMethods, error) {
70+
var s VendorPassthruMethods
71+
err := r.ExtractInto(&s)
72+
return &s, err
73+
}
74+
75+
func (r GetAllSubscriptionsVendorPassthruResult) Extract() (*GetAllSubscriptionsVendorPassthru, error) {
76+
var s GetAllSubscriptionsVendorPassthru
77+
err := r.ExtractInto(&s)
78+
return &s, err
79+
}
80+
81+
func (r SubscriptionVendorPassthruResult) Extract() (*SubscriptionVendorPassthru, error) {
82+
var s SubscriptionVendorPassthru
83+
err := r.ExtractInto(&s)
84+
return &s, err
85+
}
86+
6887
// Node represents a node in the OpenStack Bare Metal API.
6988
type Node struct {
7089
// Whether automated cleaning is enabled or disabled on this node.
@@ -323,6 +342,30 @@ type GetBIOSSettingResult struct {
323342
gophercloud.Result
324343
}
325344

345+
// VendorPassthruMethodsResult is the response from a GetVendorPassthruMethods operation. Call its Extract
346+
// method to interpret it as an array of allowed vendor methods.
347+
type VendorPassthruMethodsResult struct {
348+
gophercloud.Result
349+
}
350+
351+
// GetAllSubscriptionsVendorPassthruResult is the response from GetAllSubscriptions operation. Call its
352+
// Extract method to interpret it as a GetAllSubscriptionsVendorPassthru struct.
353+
type GetAllSubscriptionsVendorPassthruResult struct {
354+
gophercloud.Result
355+
}
356+
357+
// SubscriptionVendorPassthruResult is the response from GetSubscription and CreateSubscription operation. Call its Extract
358+
// method to interpret it as a SubscriptionVendorPassthru struct.
359+
type SubscriptionVendorPassthruResult struct {
360+
gophercloud.Result
361+
}
362+
363+
// DeleteSubscriptionVendorPassthruResult is the response from DeleteSubscription operation. Call its
364+
// ExtractErr method to determine if the call succeeded of failed.
365+
type DeleteSubscriptionVendorPassthruResult struct {
366+
gophercloud.ErrResult
367+
}
368+
326369
// Each element in the response will contain a “result” variable, which will have a value of “true” or “false”, and
327370
// also potentially a reason. A value of nil indicates that the Node’s driver does not support that interface.
328371
type DriverValidation struct {
@@ -396,3 +439,65 @@ type SingleBIOSSetting struct {
396439
type ChangeStateResult struct {
397440
gophercloud.ErrResult
398441
}
442+
443+
type VendorPassthruMethods struct {
444+
CreateSubscription CreateSubscriptionMethod `json:"create_subscription,omitempty"`
445+
DeleteSubscription DeleteSubscriptionMethod `json:"delete_subscription,omitempty"`
446+
GetSubscription GetSubscriptionMethod `json:"get_subscription,omitempty"`
447+
GetAllSubscriptions GetAllSubscriptionsMethod `json:"get_all_subscriptions,omitempty"`
448+
}
449+
450+
// Below you can find all vendor passthru methods structs
451+
452+
type CreateSubscriptionMethod struct {
453+
HTTPMethods []string `json:"http_methods"`
454+
Async bool `json:"async"`
455+
Description string `json:"description"`
456+
Attach bool `json:"attach"`
457+
RequireExclusiveLock bool `json:"require_exclusive_lock"`
458+
}
459+
460+
type DeleteSubscriptionMethod struct {
461+
HTTPMethods []string `json:"http_methods"`
462+
Async bool `json:"async"`
463+
Description string `json:"description"`
464+
Attach bool `json:"attach"`
465+
RequireExclusiveLock bool `json:"require_exclusive_lock"`
466+
}
467+
468+
type GetSubscriptionMethod struct {
469+
HTTPMethods []string `json:"http_methods"`
470+
Async bool `json:"async"`
471+
Description string `json:"description"`
472+
Attach bool `json:"attach"`
473+
RequireExclusiveLock bool `json:"require_exclusive_lock"`
474+
}
475+
476+
type GetAllSubscriptionsMethod struct {
477+
HTTPMethods []string `json:"http_methods"`
478+
Async bool `json:"async"`
479+
Description string `json:"description"`
480+
Attach bool `json:"attach"`
481+
RequireExclusiveLock bool `json:"require_exclusive_lock"`
482+
}
483+
484+
// A List of subscriptions from a node in the OpenStack Bare Metal API.
485+
type GetAllSubscriptionsVendorPassthru struct {
486+
Context string `json:"@odata.context"`
487+
Etag string `json:"@odata.etag"`
488+
Id string `json:"@odata.id"`
489+
Type string `json:"@odata.type"`
490+
Description string `json:"Description"`
491+
Name string `json:"Name"`
492+
Members []map[string]string `json:"Members"`
493+
MembersCount int `json:"Members@odata.count"`
494+
}
495+
496+
// A Subscription from a node in the OpenStack Bare Metal API.
497+
type SubscriptionVendorPassthru struct {
498+
Id string `json:"Id"`
499+
Context string `json:"Context"`
500+
Destination string `json:"Destination"`
501+
EventTypes []string `json:"EventTypes"`
502+
Protocol string `json:"Protocol"`
503+
}

0 commit comments

Comments
 (0)