github.com/vmware/govmomi@v0.43.0/vapi/library/trusted_certificates.go (about) 1 /* 2 Copyright (c) 2022-2022 VMware, Inc. All Rights Reserved. 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 library 18 19 import ( 20 "context" 21 "net/http" 22 "path" 23 24 "github.com/vmware/govmomi/vapi/internal" 25 ) 26 27 // TrustedCertificate contains a trusted certificate in Base64 encoded PEM format 28 type TrustedCertificate struct { 29 Text string `json:"cert_text"` 30 } 31 32 // TrustedCertificateSummary contains a trusted certificate in Base64 encoded PEM format and its id 33 type TrustedCertificateSummary struct { 34 TrustedCertificate 35 ID string `json:"certificate"` 36 } 37 38 // ListTrustedCertificates retrieves all content library's trusted certificates 39 func (c *Manager) ListTrustedCertificates(ctx context.Context) ([]TrustedCertificateSummary, error) { 40 url := c.Resource(internal.TrustedCertificatesPath) 41 var res struct { 42 Certificates []TrustedCertificateSummary `json:"certificates"` 43 } 44 err := c.Do(ctx, url.Request(http.MethodGet), &res) 45 return res.Certificates, err 46 } 47 48 // GetTrustedCertificate retrieves a trusted certificate for a given certificate id 49 func (c *Manager) GetTrustedCertificate(ctx context.Context, id string) (*TrustedCertificate, error) { 50 url := c.Resource(path.Join(internal.TrustedCertificatesPath, id)) 51 var res TrustedCertificate 52 err := c.Do(ctx, url.Request(http.MethodGet), &res) 53 if err != nil { 54 return nil, err 55 } 56 return &res, nil 57 } 58 59 // CreateTrustedCertificate adds a certificate to content library trust store 60 func (c *Manager) CreateTrustedCertificate(ctx context.Context, cert string) error { 61 url := c.Resource(internal.TrustedCertificatesPath) 62 body := TrustedCertificate{Text: cert} 63 return c.Do(ctx, url.Request(http.MethodPost, body), nil) 64 } 65 66 // DeleteTrustedCertificate deletes the trusted certificate from content library's trust store for the given id 67 func (c *Manager) DeleteTrustedCertificate(ctx context.Context, id string) error { 68 url := c.Resource(path.Join(internal.TrustedCertificatesPath, id)) 69 return c.Do(ctx, url.Request(http.MethodDelete), nil) 70 }