dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/common/secret.go (about) 1 // 2 // Copyright (c) 2020 Intel Corporation 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 package common 18 19 import ( 20 "encoding/json" 21 22 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 23 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors" 24 ) 25 26 // SecretDataKeyValue is a key/value pair to be stored in the Secret Store as part of the Secret Data 27 type SecretDataKeyValue struct { 28 Key string `json:"key" validate:"required"` 29 Value string `json:"value" validate:"required"` 30 } 31 32 // SecretRequest is the request DTO for storing supplied secret at a given SecretName in the Secret Store 33 type SecretRequest struct { 34 BaseRequest `json:",inline"` 35 SecretName string `json:"secretName" validate:"required"` 36 SecretData []SecretDataKeyValue `json:"secretData" validate:"required,gt=0,dive"` 37 } 38 39 // Validate satisfies the Validator interface 40 func (sr *SecretRequest) Validate() error { 41 err := common.Validate(sr) 42 return err 43 } 44 45 // UnmarshalJSON implements the Unmarshaler interface for the SecretRequest type 46 func (sr *SecretRequest) UnmarshalJSON(b []byte) error { 47 var alias struct { 48 BaseRequest 49 SecretName string 50 SecretData []SecretDataKeyValue 51 } 52 53 if err := json.Unmarshal(b, &alias); err != nil { 54 return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal SecretRequest body as JSON.", err) 55 } 56 57 *sr = SecretRequest(alias) 58 59 // validate SecretRequest DTO 60 if err := sr.Validate(); err != nil { 61 return errors.NewCommonEdgeX(errors.KindContractInvalid, "SecretRequest validation failed.", err) 62 } 63 return nil 64 } 65 66 func NewSecretRequest(secretName string, secretData []SecretDataKeyValue) SecretRequest { 67 return SecretRequest{ 68 BaseRequest: NewBaseRequest(), 69 SecretName: secretName, 70 SecretData: secretData, 71 } 72 }