github.com/leg100/ots@v0.0.7-0.20210919080622-034055ced4bd/organization.go (about) 1 package ots 2 3 import ( 4 "time" 5 6 tfe "github.com/leg100/go-tfe" 7 "gorm.io/gorm" 8 ) 9 10 const ( 11 DefaultSessionTimeout = 20160 12 DefaultSessionExpiration = 20160 13 DefaultCollaboratorAuthPolicy = "password" 14 DefaultCostEstimationEnabled = true 15 ) 16 17 var ( 18 DefaultOrganizationPermissions = tfe.OrganizationPermissions{ 19 CanCreateWorkspace: true, 20 CanUpdate: true, 21 CanDestroy: true, 22 } 23 ) 24 25 // Organization represents a Terraform Enterprise organization. 26 type Organization struct { 27 ID string 28 29 gorm.Model 30 31 Name string 32 CollaboratorAuthPolicy tfe.AuthPolicyType 33 CostEstimationEnabled bool 34 Email string 35 OwnersTeamSAMLRoleID string 36 Permissions *tfe.OrganizationPermissions 37 SAMLEnabled bool 38 SessionRemember int 39 SessionTimeout int 40 TrialExpiresAt time.Time 41 TwoFactorConformant bool 42 } 43 44 // OrganizationList represents a list of Organizations. 45 type OrganizationList struct { 46 *tfe.Pagination 47 Items []*Organization 48 } 49 50 type OrganizationService interface { 51 Create(opts *tfe.OrganizationCreateOptions) (*Organization, error) 52 Get(name string) (*Organization, error) 53 List(opts tfe.OrganizationListOptions) (*OrganizationList, error) 54 Update(name string, opts *tfe.OrganizationUpdateOptions) (*Organization, error) 55 Delete(name string) error 56 GetEntitlements(name string) (*Entitlements, error) 57 } 58 59 type OrganizationStore interface { 60 Create(org *Organization) (*Organization, error) 61 Get(name string) (*Organization, error) 62 List(opts tfe.OrganizationListOptions) (*OrganizationList, error) 63 Update(name string, fn func(*Organization) error) (*Organization, error) 64 Delete(name string) error 65 } 66 67 func NewOrganization(opts *tfe.OrganizationCreateOptions) (*Organization, error) { 68 org := Organization{ 69 Name: *opts.Name, 70 Email: *opts.Email, 71 ID: GenerateID("org"), 72 SessionTimeout: DefaultSessionTimeout, 73 SessionRemember: DefaultSessionExpiration, 74 CollaboratorAuthPolicy: DefaultCollaboratorAuthPolicy, 75 CostEstimationEnabled: DefaultCostEstimationEnabled, 76 Permissions: &DefaultOrganizationPermissions, 77 } 78 79 if opts.SessionTimeout != nil { 80 org.SessionTimeout = *opts.SessionTimeout 81 } 82 83 if opts.SessionRemember != nil { 84 org.SessionRemember = *opts.SessionRemember 85 } 86 87 if opts.CollaboratorAuthPolicy != nil { 88 org.CollaboratorAuthPolicy = *opts.CollaboratorAuthPolicy 89 } 90 91 if opts.CostEstimationEnabled != nil { 92 org.CostEstimationEnabled = *opts.CostEstimationEnabled 93 } 94 95 return &org, nil 96 } 97 98 func UpdateOrganization(org *Organization, opts *tfe.OrganizationUpdateOptions) error { 99 if opts.Name != nil { 100 org.Name = *opts.Name 101 } 102 103 if opts.Email != nil { 104 org.Email = *opts.Email 105 } 106 107 if opts.SessionTimeout != nil { 108 org.SessionTimeout = *opts.SessionTimeout 109 } 110 111 if opts.SessionRemember != nil { 112 org.SessionRemember = *opts.SessionRemember 113 } 114 115 if opts.CollaboratorAuthPolicy != nil { 116 org.CollaboratorAuthPolicy = *opts.CollaboratorAuthPolicy 117 } 118 119 if opts.CostEstimationEnabled != nil { 120 org.CostEstimationEnabled = *opts.CostEstimationEnabled 121 } 122 123 return nil 124 }