| 1 | package kongscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src go1.12 context/" |
| 5 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/" |
| 6 | ) |
| 7 | |
| 8 | // KeyAuthService handles key-auth credentials in Kong. |
| 9 | type KeyAuthServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService# servicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service# |
| 10 | |
| 11 | // Create creates a key-auth credential in Kong |
| 12 | // If an ID is specified, it will be used to |
| 13 | // create a key-auth in Kong, otherwise an ID |
| 14 | // is auto-generated. |
| 15 | func (s *KeyAuthServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#) Createscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#Create().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, |
| 16 | consumerUsernameOrID *string, keyAuth *KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth#) (*KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth#, error) { |
| 17 | |
| 18 | cred, err := s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..credentialsscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#credentials..Createscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#Create().(ctx, "key-auth", |
| 19 | consumerUsernameOrID, keyAuth) |
| 20 | if err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | |
| 24 | var createdKeyAuth KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth# |
| 25 | err = jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/Unmarshal().(cred, &createdKeyAuth) |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | |
| 30 | return &createdKeyAuth, nil |
| 31 | } |
| 32 | |
| 33 | // Get fetches a key-auth credential from Kong. |
| 34 | func (s *KeyAuthServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#) Getscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#Get().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, |
| 35 | consumerUsernameOrID, keyOrID *string) (*KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth#, error) { |
| 36 | |
| 37 | cred, err := s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..credentialsscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#credentials..Getscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#Get().(ctx, "key-auth", |
| 38 | consumerUsernameOrID, keyOrID) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | |
| 43 | var keyAuth KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth# |
| 44 | err = jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/Unmarshal().(cred, &keyAuth) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | |
| 49 | return &keyAuth, nil |
| 50 | } |
| 51 | |
| 52 | // Update updates a key-auth credential in Kong |
| 53 | func (s *KeyAuthServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#) Updatescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#Update().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, |
| 54 | consumerUsernameOrID *string, keyAuth *KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth#) (*KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth#, error) { |
| 55 | |
| 56 | cred, err := s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..credentialsscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#credentials..Updatescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#Update().(ctx, "key-auth", |
| 57 | consumerUsernameOrID, keyAuth) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | var updatedKeyAuth KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth# |
| 63 | err = jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/Unmarshal().(cred, &updatedKeyAuth) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | |
| 68 | return &updatedKeyAuth, nil |
| 69 | } |
| 70 | |
| 71 | // Delete deletes a key-auth credential in Kong |
| 72 | func (s *KeyAuthServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#) Deletescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#Delete().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, |
| 73 | consumerUsernameOrID, keyOrID *string) error { |
| 74 | return s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..credentialsscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#credentials..Deletescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/credentialService#Delete().(ctx, "key-auth", |
| 75 | consumerUsernameOrID, keyOrID) |
| 76 | } |
| 77 | |
| 78 | // List fetches a list of key-auth credentials in Kong. |
| 79 | // opt can be used to control pagination. |
| 80 | func (s *KeyAuthServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#) Listscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#List().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, |
| 81 | opt *ListOptscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/ListOpt#) ([]*KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth#, *ListOptscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/ListOpt#, error) { |
| 82 | data, next, err := s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..listscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#list().(ctx, "/key-auths", opt) |
| 83 | if err != nil { |
| 84 | return nil, nil, err |
| 85 | } |
| 86 | var keyAuths []*KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth# |
| 87 | for _, object := range data { |
| 88 | b, err := object.MarshalJSONscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/RawMessage#MarshalJSON().() |
| 89 | if err != nil { |
| 90 | return nil, nil, err |
| 91 | } |
| 92 | var keyAuth KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth# |
| 93 | err = jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/Unmarshal().(b, &keyAuth) |
| 94 | if err != nil { |
| 95 | return nil, nil, err |
| 96 | } |
| 97 | keyAuths = append(keyAuths, &keyAuth) |
| 98 | } |
| 99 | |
| 100 | return keyAuths, next, nil |
| 101 | } |
| 102 | |
| 103 | // ListAll fetches all key-auth credentials in Kong. |
| 104 | // This method can take a while if there |
| 105 | // a lot of key-auth credentials present. |
| 106 | func (s *KeyAuthServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#) ListAllscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#ListAll().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#) ([]*KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth#, error) { |
| 107 | var keyAuths, data []*KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth# |
| 108 | var err error |
| 109 | opt := &ListOptscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/ListOpt#{Sizescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/ListOpt#Size.: pageSizescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/pageSize.} |
| 110 | |
| 111 | for opt != nil { |
| 112 | data, opt, err = s.Listscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#List().(ctx, opt) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | keyAuths = append(keyAuths, data...) |
| 117 | } |
| 118 | return keyAuths, nil |
| 119 | } |
| 120 | |
| 121 | // ListForConsumer fetches a list of key-auth credentials |
| 122 | // in Kong associated with a specific consumer. |
| 123 | // opt can be used to control pagination. |
| 124 | func (s *KeyAuthServicescip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#) ListForConsumerscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuthService#ListForConsumer().(ctx contextscip-go gomod github.com/golang/go/src go1.12 context/.Contextscip-go gomod github.com/golang/go/src go1.12 context/Context#, |
| 125 | consumerUsernameOrID *string, opt *ListOptscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/ListOpt#) ([]*KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth#, *ListOptscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/ListOpt#, error) { |
| 126 | data, next, err := s.clientscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/service#client..listscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/Client#list().(ctx, |
| 127 | "/consumers/"+*consumerUsernameOrID+"/key-auth", opt) |
| 128 | if err != nil { |
| 129 | return nil, nil, err |
| 130 | } |
| 131 | var keyAuths []*KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth# |
| 132 | for _, object := range data { |
| 133 | b, err := object.MarshalJSONscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/RawMessage#MarshalJSON().() |
| 134 | if err != nil { |
| 135 | return nil, nil, err |
| 136 | } |
| 137 | var keyAuth KeyAuthscip-go gomod github.com/kong/go-kong . `github.com/kong/go-kong/kong`/KeyAuth# |
| 138 | err = jsonscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.12 `encoding/json`/Unmarshal().(b, &keyAuth) |
| 139 | if err != nil { |
| 140 | return nil, nil, err |
| 141 | } |
| 142 | keyAuths = append(keyAuths, &keyAuth) |
| 143 | } |
| 144 | |
| 145 | return keyAuths, next, nil |
| 146 | } |