github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/tools/lxdclient/client_cert.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build go1.3
     5  
     6  package lxdclient
     7  
     8  import (
     9  	"crypto/x509"
    10  	"net/http"
    11  
    12  	"github.com/juju/errors"
    13  	"github.com/lxc/lxd"
    14  	"github.com/lxc/lxd/shared"
    15  )
    16  
    17  type rawCertClient interface {
    18  	CertificateList() ([]shared.CertInfo, error)
    19  	CertificateAdd(cert *x509.Certificate, name string) error
    20  	CertificateRemove(fingerprint string) error
    21  }
    22  
    23  type certClient struct {
    24  	raw rawCertClient
    25  }
    26  
    27  // AddCert adds the given certificate to the server.
    28  func (c certClient) AddCert(cert Cert) error {
    29  	x509Cert, err := cert.X509()
    30  	if err != nil {
    31  		return errors.Trace(err)
    32  	}
    33  
    34  	if err := c.raw.CertificateAdd(x509Cert, cert.Name); err != nil {
    35  		return errors.Trace(err)
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  // RemoveCertByFingerprint removes the cert from the server.
    42  func (c certClient) RemoveCertByFingerprint(fingerprint string) error {
    43  	if err := c.raw.CertificateRemove(fingerprint); err != nil {
    44  		if err == lxd.LXDErrors[http.StatusNotFound] {
    45  			return errors.NotFoundf("certificate with fingerprint %q", fingerprint)
    46  		}
    47  		return errors.Trace(err)
    48  	}
    49  	return nil
    50  }