github.com/vmware/govmomi@v0.51.0/vapi/library/trusted_certificates.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package library
     6  
     7  import (
     8  	"context"
     9  	"net/http"
    10  	"path"
    11  
    12  	"github.com/vmware/govmomi/vapi/internal"
    13  )
    14  
    15  // TrustedCertificate contains a trusted certificate in Base64 encoded PEM format
    16  type TrustedCertificate struct {
    17  	Text string `json:"cert_text"`
    18  }
    19  
    20  // TrustedCertificateSummary contains a trusted certificate in Base64 encoded PEM format and its id
    21  type TrustedCertificateSummary struct {
    22  	TrustedCertificate
    23  	ID string `json:"certificate"`
    24  }
    25  
    26  // ListTrustedCertificates retrieves all content library's trusted certificates
    27  func (c *Manager) ListTrustedCertificates(ctx context.Context) ([]TrustedCertificateSummary, error) {
    28  	url := c.Resource(internal.TrustedCertificatesPath)
    29  	var res struct {
    30  		Certificates []TrustedCertificateSummary `json:"certificates"`
    31  	}
    32  	err := c.Do(ctx, url.Request(http.MethodGet), &res)
    33  	return res.Certificates, err
    34  }
    35  
    36  // GetTrustedCertificate retrieves a trusted certificate for a given certificate id
    37  func (c *Manager) GetTrustedCertificate(ctx context.Context, id string) (*TrustedCertificate, error) {
    38  	url := c.Resource(path.Join(internal.TrustedCertificatesPath, id))
    39  	var res TrustedCertificate
    40  	err := c.Do(ctx, url.Request(http.MethodGet), &res)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	return &res, nil
    45  }
    46  
    47  // CreateTrustedCertificate adds a certificate to content library trust store
    48  func (c *Manager) CreateTrustedCertificate(ctx context.Context, cert string) error {
    49  	url := c.Resource(internal.TrustedCertificatesPath)
    50  	body := TrustedCertificate{Text: cert}
    51  	return c.Do(ctx, url.Request(http.MethodPost, body), nil)
    52  }
    53  
    54  // DeleteTrustedCertificate deletes the trusted certificate from content library's trust store for the given id
    55  func (c *Manager) DeleteTrustedCertificate(ctx context.Context, id string) error {
    56  	url := c.Resource(path.Join(internal.TrustedCertificatesPath, id))
    57  	return c.Do(ctx, url.Request(http.MethodDelete), nil)
    58  }