Skip to content

Commit

Permalink
feat: adds windows OS resources (#25)
Browse files Browse the repository at this point in the history
* feat(windows-os): Windows OS - implementation
* implementation Data provider
* fix(windows-os): double naming
* docs(windows-os): templating documentation and examples

---------

Co-authored-by: Maximilian.Brand <[email protected]>
  • Loading branch information
atddo and cancom-mbrand authored Nov 7, 2023
1 parent 77b47d0 commit 28a33bb
Show file tree
Hide file tree
Showing 14 changed files with 516 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cancom/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func Provider() *schema.Provider {

ar := aggregateResources()

dr := aggregateData()

return &schema.Provider{
Schema: map[string]*schema.Schema{
"token": {
Expand All @@ -38,7 +40,7 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: ar,
DataSourcesMap: map[string]*schema.Resource{},
DataSourcesMap: dr,
ConfigureContextFunc: providerConfigure,
}
}
Expand Down
14 changes: 14 additions & 0 deletions cancom/service_aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cancom/terraform-provider-cancom/cancom/services/iam"
"github.com/cancom/terraform-provider-cancom/cancom/services/ipam"
sslmonitoring "github.com/cancom/terraform-provider-cancom/cancom/services/ssl-monitoring"
windowsos "github.com/cancom/terraform-provider-cancom/cancom/services/windows-os"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -17,6 +18,7 @@ func getAllProviders() []base.ProviderI {
iam.New(),
ipam.New(),
cmsmgw.New(),
windowsos.New(),
}
}

Expand All @@ -31,3 +33,15 @@ func aggregateResources() map[string]*schema.Resource {
}
return resources
}

func aggregateData() map[string]*schema.Resource {
p := getAllProviders()
datas := make(map[string]*schema.Resource)
for _, p := range p {
for k, v := range p.Provider().DataSourcesMap {
k = "cancom_" + p.Name() + "_" + k
datas[k] = v
}
}
return datas
}
38 changes: 38 additions & 0 deletions cancom/services/windows-os/data_deployment_progess.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package windowsos

import (
"github.com/cancom/terraform-provider-cancom/client"
client_windowsos "github.com/cancom/terraform-provider-cancom/client/services/windows-os"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataWindowsOSDeploymentProgress() *schema.Resource {
return &schema.Resource{

Read: WindowsOSDeploymentProgressRead,
Schema: map[string]*schema.Schema{
"deployment_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the deployment object.",
},
},
}
}

func WindowsOSDeploymentProgressRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*client.Client)

c.HostURL = c.ServiceURLs["managed-windows"]

resp, err := (*client_windowsos.Client)(c).CreateWindowsDeploymentStatus(d.Get("deployment_id").(string))
if err != nil {
return err
}

d.SetId(resp.Id)

return nil

}
30 changes: 30 additions & 0 deletions cancom/services/windows-os/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package windowsos

import (
"github.com/cancom/terraform-provider-cancom/cancom/services/base"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type Provider base.Provider

func (p Provider) Name() string {
return p.ServiceName
}

func (p Provider) Provider() *schema.Provider {
return p.ProviderSchema
}

func New() Provider {
return Provider{
ServiceName: "windows_os",
ProviderSchema: &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"deployment": resourceWindowsOSDeployment(),
},
DataSourcesMap: map[string]*schema.Resource{
"deployment_progress": dataWindowsOSDeploymentProgress(),
},
},
}
}
159 changes: 159 additions & 0 deletions cancom/services/windows-os/resource_deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package windowsos

import (
"context"

"github.com/cancom/terraform-provider-cancom/client"
client_windowsos "github.com/cancom/terraform-provider-cancom/client/services/windows-os"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceWindowsOSDeployment() *schema.Resource {
return &schema.Resource{
CreateContext: resourceWindowsOSCreate,
ReadContext: resourceWindowsOSRead,
UpdateContext: resourceWindowsOSUpdate,
DeleteContext: resourceWindowsOSDelete,
Schema: map[string]*schema.Schema{
"customer_environment_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the customer environment.",
},
"role": {
Type: schema.TypeString,
Required: true,
Description: "Role of the Machine - needs to match predefined roles.",
},
"maintenance_window_id": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of maintenanceWindow IDs",
},
"services": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Services deployed and delivered to maschine",
},
"customer_number": {
Type: schema.TypeString,
Required: false,
Computed: true,
Description: "Customer number for Windows Management",
},
},
}
}

func resourceWindowsOSCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Client)

c.HostURL = c.ServiceURLs["managed-windows"]

var diags diag.Diagnostics

tempservices := d.Get("services").([]interface{})
servicesarray := []string{}
for _, tempservices := range tempservices {
servicesarray = append(servicesarray, tempservices.(string))
}

tempmaintenance_window_id := d.Get("maintenance_window_id").([]interface{})
maintenance_window_id_array := []string{}
for _, tempmaintenance_window_id := range tempmaintenance_window_id {
maintenance_window_id_array = append(maintenance_window_id_array, tempmaintenance_window_id.(string))
}
computerObject := client_windowsos.WindowsOS_Computer{
Services: servicesarray,
MaintenanceWindowId: maintenance_window_id_array,
Role: d.Get("role").(string),
}

windowsOSRequest := client_windowsos.WindowsOS_Deplyoment{

Computer: computerObject,
CoustomerEnvironmentID: d.Get("customer_environment_id").(string),
}

resp, err := (*client_windowsos.Client)(c).CreateWindowsDeployment(&windowsOSRequest)
if err != nil {
return diag.FromErr(err)
}

d.Set("customer_number", resp.Computer.CustomerID)
d.SetId(resp.Id)

return diags

}

func resourceWindowsOSRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Client)

c.HostURL = c.ServiceURLs["managed-windows"]

var diags diag.Diagnostics

resp, err := (*client_windowsos.Client)(c).GetWindowsDeployment(d.Id())
if err != nil {
return diag.FromErr(err)
}

d.Set("customer_environment_id", resp.CoustomerEnvironmentID)
d.Set("role", resp.Computer.Role)
d.Set("maintenance_window_id", resp.Computer.MaintenanceWindowId)
d.Set("services", resp.Computer.Services)

return diags
}

func resourceWindowsOSUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Client)

c.HostURL = c.ServiceURLs["managed-windows"]

var diags diag.Diagnostics

computerObject := client_windowsos.WindowsOS_Computer{
Services: d.Get("services").([]string),
MaintenanceWindowId: d.Get("maintenance_window_id").([]string),
Role: d.Get("role").(string),
}

windowsOSRequest := client_windowsos.WindowsOS_Deplyoment{
Computer: computerObject,
CoustomerEnvironmentID: d.Get("customer_environment_id").(string),
}

resp, err := (*client_windowsos.Client)(c).UpdateWindowsOsDeployment(d.Id(), &windowsOSRequest)
if err != nil {
return diag.FromErr(err)
}

d.SetId(resp.Id)

return diags
}

func resourceWindowsOSDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Client)

c.HostURL = c.ServiceURLs["managed-windows"]

var diags diag.Diagnostics

err := (*client_windowsos.Client)(c).DeleteWindowsDeployment(d.Id())
if err != nil {
return diag.FromErr(err)
}

return diags
}
1 change: 1 addition & 0 deletions client/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ var (
DnsURL = "https://vpswqliuu8.execute-api.eu-central-1.amazonaws.com/dev/v1"
CmsMgwHostURL = "https://cmsmgw.cc-mase-interfaces.de/dev"
IpManagementHostURL = "https://ip-management.portal.cancom.io"
WindowsOSURL = "https://windows-os.cancom.io"
)
16 changes: 16 additions & 0 deletions client/services/windows-os/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package client_windowsos

type WindowsOS_Deplyoment struct {
Status int `json:"status,omitempty"`
Id string `json:"deploymentId,omitempty"`
CoustomerEnvironmentID string `json:"coustomerEnvironmentID"`
Computer WindowsOS_Computer `json:"computer"`
}
type WindowsOS_Computer struct {
Computername string `json:"computername,omitempty"`
Guid string `json:"guid,omitempty"`
Services []string `json:"services"`
CustomerID string `json:"customerID,omitempty"`
MaintenanceWindowId []string `json:"maintenanceWindowId"`
Role string `json:"role"`
}
Loading

0 comments on commit 28a33bb

Please sign in to comment.