github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/cs/desired.go (about)

     1  package cs
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/caos/orbos/mntr"
     8  
     9  	"github.com/caos/orbos/pkg/secret"
    10  	"github.com/caos/orbos/pkg/tree"
    11  )
    12  
    13  type Desired struct {
    14  	Common        *tree.Common `yaml:",inline"`
    15  	Spec          Spec
    16  	Loadbalancing *tree.Tree
    17  }
    18  
    19  type Pool struct {
    20  	Flavor       string
    21  	Zone         string
    22  	VolumeSizeGB int
    23  }
    24  
    25  func (p Pool) validate() error {
    26  	return nil
    27  }
    28  
    29  type Spec struct {
    30  	Verbose             bool
    31  	APIToken            *secret.Secret `yaml:",omitempty"`
    32  	Pools               map[string]*Pool
    33  	SSHKey              *SSHKey
    34  	RebootRequired      []string
    35  	ReplacementRequired []string
    36  }
    37  
    38  type SSHKey struct {
    39  	Private *secret.Secret `yaml:",omitempty"`
    40  	Public  *secret.Secret `yaml:",omitempty"`
    41  }
    42  
    43  func (d Desired) validateAdapt() (err error) {
    44  
    45  	defer func() {
    46  		err = mntr.ToUserError(err)
    47  	}()
    48  
    49  	if d.Loadbalancing == nil {
    50  		return errors.New("no loadbalancing configured")
    51  	}
    52  	if len(d.Spec.Pools) == 0 {
    53  		return errors.New("no pools configured")
    54  	}
    55  	for poolName, pool := range d.Spec.Pools {
    56  		if err := pool.validate(); err != nil {
    57  			return fmt.Errorf("configuring pool %s failed: %w", poolName, err)
    58  		}
    59  	}
    60  	return nil
    61  }
    62  
    63  func (d Desired) validateAPIToken() error {
    64  	if d.Spec.APIToken == nil ||
    65  		d.Spec.APIToken.Value == "" {
    66  		return mntr.ToUserError(errors.New("apitoken missing... please provide a cloudscale api token using orbctl writesecret command"))
    67  	}
    68  	return nil
    69  }
    70  
    71  func (d Desired) validateQuery() (err error) {
    72  	defer func() {
    73  		err = mntr.ToUserError(err)
    74  	}()
    75  
    76  	if err := d.validateAPIToken(); err != nil {
    77  		return err
    78  	}
    79  
    80  	if d.Spec.SSHKey.Private == nil ||
    81  		d.Spec.SSHKey.Private.Value == "" ||
    82  		d.Spec.SSHKey.Public == nil ||
    83  		d.Spec.SSHKey.Public.Value == "" {
    84  		return errors.New("ssh key missing... please initialize your orb using orbctl configure command")
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  func parseDesired(desiredTree *tree.Tree) (*Desired, error) {
    91  	desiredKind := &Desired{
    92  		Common: desiredTree.Common,
    93  		Spec:   Spec{},
    94  	}
    95  
    96  	if err := desiredTree.Original.Decode(desiredKind); err != nil {
    97  		return nil, mntr.ToUserError(fmt.Errorf("parsing desired state failed: %w", err))
    98  	}
    99  
   100  	return desiredKind, nil
   101  }