Skip to content

Commit

Permalink
Merge pull request #24 from karimra/api-os
Browse files Browse the repository at this point in the history
add os RPCs api
  • Loading branch information
karimra authored May 18, 2022
2 parents 5ac382f + 6c2b3cb commit ac6453d
Show file tree
Hide file tree
Showing 6 changed files with 454 additions and 31 deletions.
54 changes: 23 additions & 31 deletions api/cert/options.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package cert

import (
"errors"
"strings"

"github.com/karimra/gnoic/api"
"github.com/openconfig/gnoi/cert"
"google.golang.org/protobuf/proto"
)

type CertOption func(proto.Message) error

// ErrInvalidMsgType is returned by a CertOption in case the Option is supplied
// an unexpected proto.Message
var ErrInvalidMsgType = errors.New("invalid message type")

// ErrInvalidValue is returned by a CertOption in case the Option is supplied
// an unexpected value.
var ErrInvalidValue = errors.New("invalid value")

// apply is a helper function that simply applies the options to the proto.Message.
// It returns an error if any of the options fails.
func apply(m proto.Message, opts ...CertOption) error {
Expand All @@ -32,11 +24,11 @@ func apply(m proto.Message, opts ...CertOption) error {
func CertificateType(ct string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
ctv, ok := cert.CertificateType_value[strings.ToUpper(ct)]
if !ok {
return ErrInvalidValue
return api.ErrInvalidValue
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CanGenerateCSRRequest:
Expand All @@ -55,7 +47,7 @@ func CertificateType(ct string) func(msg proto.Message) error {
func CertificateInfo(opts ...CertOption) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.GetCertificatesResponse:
Expand All @@ -76,7 +68,7 @@ func CertificateInfo(opts ...CertOption) func(msg proto.Message) error {
func Certificate(opts ...CertOption) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.LoadCertificateRequest:
Expand All @@ -101,7 +93,7 @@ func Certificate(opts ...CertOption) func(msg proto.Message) error {
func CertificateID(id string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.GenerateCSRRequest:
Expand Down Expand Up @@ -130,7 +122,7 @@ func CertificateID(id string) func(msg proto.Message) error {
func CaCertificate(opts ...CertOption) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.LoadCertificateRequest:
Expand Down Expand Up @@ -161,7 +153,7 @@ func CaCertificate(opts ...CertOption) func(msg proto.Message) error {
func ErrorMsg(s string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CertificateRevocationError:
Expand All @@ -174,7 +166,7 @@ func ErrorMsg(s string) func(msg proto.Message) error {
func CSRParams(opts ...CertOption) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.GenerateCSRRequest:
Expand All @@ -192,7 +184,7 @@ func CSRParams(opts ...CertOption) func(msg proto.Message) error {
func CSR(opts ...CertOption) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.GenerateCSRResponse:
Expand All @@ -210,7 +202,7 @@ func CSR(opts ...CertOption) func(msg proto.Message) error {
func KeySize(ks uint32) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CanGenerateCSRRequest:
Expand All @@ -229,13 +221,13 @@ func MinKeySize(ks uint32) func(msg proto.Message) error {
func KeyType(kt string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CanGenerateCSRRequest:
ktv, ok := cert.KeyType_value[strings.ToUpper(kt)]
if !ok {
return ErrInvalidValue
return api.ErrInvalidValue
}
msg.KeyType = cert.KeyType(ktv)
}
Expand All @@ -246,7 +238,7 @@ func KeyType(kt string) func(msg proto.Message) error {
func CommonName(cn string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CSRParams:
Expand All @@ -259,7 +251,7 @@ func CommonName(cn string) func(msg proto.Message) error {
func Country(c string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CSRParams:
Expand All @@ -272,7 +264,7 @@ func Country(c string) func(msg proto.Message) error {
func State(s string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CSRParams:
Expand All @@ -285,7 +277,7 @@ func State(s string) func(msg proto.Message) error {
func City(s string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CSRParams:
Expand All @@ -298,7 +290,7 @@ func City(s string) func(msg proto.Message) error {
func Org(s string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CSRParams:
Expand All @@ -311,7 +303,7 @@ func Org(s string) func(msg proto.Message) error {
func OrgUnit(s string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CSRParams:
Expand All @@ -324,7 +316,7 @@ func OrgUnit(s string) func(msg proto.Message) error {
func IPAddress(ipAddr string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CSRParams:
Expand All @@ -337,7 +329,7 @@ func IPAddress(ipAddr string) func(msg proto.Message) error {
func EmailID(s string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CSRParams:
Expand All @@ -350,7 +342,7 @@ func EmailID(s string) func(msg proto.Message) error {
func Endpoint(typ cert.Endpoint_Type, endp string) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CertificateInfo:
Expand All @@ -369,7 +361,7 @@ func Endpoint(typ cert.Endpoint_Type, endp string) func(msg proto.Message) error
func ModificationTime(mt int64) func(msg proto.Message) error {
return func(msg proto.Message) error {
if msg == nil {
return ErrInvalidMsgType
return api.ErrInvalidMsgType
}
switch msg := msg.ProtoReflect().Interface().(type) {
case *cert.CertificateInfo:
Expand Down
11 changes: 11 additions & 0 deletions api/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package api

import "errors"

// ErrInvalidMsgType is returned by an Option in case the Option is supplied
// an unexpected proto.Message
var ErrInvalidMsgType = errors.New("invalid message type")

// ErrInvalidValue is returned by a Option in case the Option is supplied
// an unexpected value.
var ErrInvalidValue = errors.New("invalid value")
33 changes: 33 additions & 0 deletions api/os/activate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package os

import gnoios "github.com/openconfig/gnoi/os"

func NewActivateRequest(opts ...OsOption) (*gnoios.ActivateRequest, error) {
m := new(gnoios.ActivateRequest)
err := apply(m, opts...)
if err != nil {
return nil, err
}
return m, nil
}

func NewActivateOKResponse() *gnoios.ActivateResponse {
return &gnoios.ActivateResponse{
Response: &gnoios.ActivateResponse_ActivateOk{
ActivateOk: &gnoios.ActivateOK{},
},
}
}

func NewActivateErrorResponse(opts ...OsOption) (*gnoios.ActivateResponse, error) {
m := new(gnoios.ActivateError)
err := apply(m, opts...)
if err != nil {
return nil, err
}
return &gnoios.ActivateResponse{
Response: &gnoios.ActivateResponse_ActivateError{
ActivateError: m,
},
}, nil
}
136 changes: 136 additions & 0 deletions api/os/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package os

import (
gnoios "github.com/openconfig/gnoi/os"
)

func NewOSInstallTransferRequest(opts ...OsOption) (*gnoios.InstallRequest, error) {
m, err := NewOSTransferRequest(opts...)
if err != nil {
return nil, err
}
return &gnoios.InstallRequest{
Request: &gnoios.InstallRequest_TransferRequest{
TransferRequest: m,
},
}, nil

}

func NewOSInstallTransferContent(opts ...OsOption) (*gnoios.InstallRequest, error) {
m := new(gnoios.InstallRequest)
err := apply(m, opts...)
if err != nil {
return nil, err
}
return m, nil
}

func NewOSInstallTransferEnd() *gnoios.InstallRequest {
return &gnoios.InstallRequest{
Request: &gnoios.InstallRequest_TransferEnd{
TransferEnd: &gnoios.TransferEnd{},
},
}
}

func NewOSInstallTransferReadyResponse() *gnoios.InstallResponse {
return &gnoios.InstallResponse{
Response: &gnoios.InstallResponse_TransferReady{
TransferReady: &gnoios.TransferReady{},
},
}
}

func NewOSInstallTransferProgressResponse(opts ...OsOption) (*gnoios.InstallResponse, error) {
m, err := NewOSTransferProgress(opts...)
if err != nil {
return nil, err
}
return &gnoios.InstallResponse{
Response: &gnoios.InstallResponse_TransferProgress{
TransferProgress: m,
},
}, nil
}

func NewOSInstallSyncProgressResponse(opts ...OsOption) (*gnoios.InstallResponse, error) {
m, err := NewOSSyncProgress(opts...)
if err != nil {
return nil, err
}
return &gnoios.InstallResponse{
Response: &gnoios.InstallResponse_SyncProgress{
SyncProgress: m,
},
}, nil
}

func NewOSInstallValidatedResponse(opts ...OsOption) (*gnoios.InstallResponse, error) {
m, err := NewOSValidated(opts...)
if err != nil {
return nil, err
}
return &gnoios.InstallResponse{
Response: &gnoios.InstallResponse_Validated{
Validated: m,
},
}, nil
}

func NewOSInstallInstallErrorResponse(opts ...OsOption) (*gnoios.InstallResponse, error) {
m, err := NewOSInstallError(opts...)
if err != nil {
return nil, err
}
return &gnoios.InstallResponse{
Response: &gnoios.InstallResponse_InstallError{
InstallError: m,
},
}, nil
}

func NewOSTransferRequest(opts ...OsOption) (*gnoios.TransferRequest, error) {
m := new(gnoios.TransferRequest)
err := apply(m, opts...)
if err != nil {
return nil, err
}
return m, nil
}

func NewOSTransferProgress(opts ...OsOption) (*gnoios.TransferProgress, error) {
m := new(gnoios.TransferProgress)
err := apply(m, opts...)
if err != nil {
return nil, err
}
return m, nil
}

func NewOSSyncProgress(opts ...OsOption) (*gnoios.SyncProgress, error) {
m := new(gnoios.SyncProgress)
err := apply(m, opts...)
if err != nil {
return nil, err
}
return m, nil
}

func NewOSValidated(opts ...OsOption) (*gnoios.Validated, error) {
m := new(gnoios.Validated)
err := apply(m, opts...)
if err != nil {
return nil, err
}
return m, nil
}

func NewOSInstallError(opts ...OsOption) (*gnoios.InstallError, error) {
m := new(gnoios.InstallError)
err := apply(m, opts...)
if err != nil {
return nil, err
}
return m, nil
}
Loading

0 comments on commit ac6453d

Please sign in to comment.