github.com/Venafi/vcert/v5@v5.10.2/pkg/playbook/app/domain/certificateTask.go (about) 1 /* 2 * Copyright 2023 Venafi, Inc. 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 domain 18 19 import ( 20 "errors" 21 "fmt" 22 ) 23 24 // CertificateTask represents a task to be run: 25 // A certificate to be requested/renewed and installed in one (or more) location(s) 26 type CertificateTask struct { 27 Name string `yaml:"name,omitempty"` 28 Request PlaybookRequest `yaml:"request,omitempty"` 29 Installations Installations `yaml:"installations,omitempty"` 30 RenewBefore string `yaml:"renewBefore,omitempty"` 31 SetEnvVars []string `yaml:"setEnvVars,omitempty"` 32 } 33 34 // CertificateTasks is a slice of CertificateTask 35 type CertificateTasks []CertificateTask 36 37 // IsValid returns true if the CertificateTask has the minimum required fields to be run 38 func (task CertificateTask) IsValid() (bool, error) { 39 var rErr error = nil 40 rValid := true 41 42 // Each certificate request needs a zone, required field 43 if task.Request.Zone == "" { 44 rValid = false 45 rErr = errors.Join(rErr, fmt.Errorf("\t\t%w", ErrNoRequestZone)) 46 } 47 48 if task.Request.Subject.CommonName == "" { 49 rValid = false 50 rErr = errors.Join(rErr, fmt.Errorf("\t\t%w", ErrNoRequestCN)) 51 } 52 53 // This task has no installations defined 54 if len(task.Installations) < 1 { 55 rValid = false 56 rErr = errors.Join(rErr, fmt.Errorf("\t\t%w", ErrNoInstallations)) 57 } 58 59 // Validate each installation 60 for i, installation := range task.Installations { 61 _, err := installation.IsValid() 62 if err != nil { 63 rErr = errors.Join(rErr, fmt.Errorf("\t\tinstallations[%d]:\n%w", i, err)) 64 rValid = false 65 } 66 } 67 68 return rValid, rErr 69 }