Skip to content

Commit

Permalink
squash commits into one for coreos host attach support
Browse files Browse the repository at this point in the history
  • Loading branch information
bhpratt authored and hkantare committed Aug 17, 2022
1 parent f16f2fe commit e5cb049
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/IBM-Cloud/bluemix-go v0.0.0-20220523145737-34645883de47
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20220622142911-811d18c8c775
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20220728135852-60ff775f7a8d
github.com/IBM-Cloud/power-go-client v1.1.11
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca
github.com/IBM/appconfiguration-go-admin-sdk v0.3.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/IBM-Cloud/bluemix-go v0.0.0-20220523145737-34645883de47 h1:lpClRYyGuSXX4m3PRO2fruDQiesshUrObgbnPi+Xbxk=
github.com/IBM-Cloud/bluemix-go v0.0.0-20220523145737-34645883de47/go.mod h1:tfNN3lCKuA2+SQvndt0+5CjPr2qn/wdNLjrue1GrOhY=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20220622142911-811d18c8c775 h1:hTj8cRqWv76Io/j+fUKq0Rrq1AjBA7uvRLs634ea/+8=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20220622142911-811d18c8c775/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20220728135852-60ff775f7a8d h1:cHu5Iev9ggo1fktwmHbmPqDOkt3VYmdUGn1U7/Zb238=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20220728135852-60ff775f7a8d/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.5.3/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs=
github.com/IBM-Cloud/power-go-client v1.1.10 h1:NUGZwF5V0j2lFurA5LaigsYyOKDKwz4M0sCpm+YIbig=
github.com/IBM-Cloud/power-go-client v1.1.10/go.mod h1:Qfx0fNi+9hms+xu9Z6Euhu9088ByW6C/TCMLECTRWNE=
github.com/IBM-Cloud/power-go-client v1.1.11 h1:/qTWCCuSZsmiksvQSfhM+mZKkY/Vli/W6b82WoYD2NM=
github.com/IBM-Cloud/power-go-client v1.1.11/go.mod h1:Qfx0fNi+9hms+xu9Z6Euhu9088ByW6C/TCMLECTRWNE=
github.com/IBM-Cloud/softlayer-go v1.0.5-tf h1:koUAyF9b6X78lLLruGYPSOmrfY2YcGYKOj/Ug9nbKNw=
Expand Down
62 changes: 44 additions & 18 deletions ibm/service/satellite/data_source_ibm_satellite_host_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func DataSourceIBMSatelliteAttachHostScript() *schema.Resource {
Computed: true,
Description: "A unique name for the new Satellite location",
},
"coreos_host": {
Type: schema.TypeBool,
Optional: true,
Description: "If true, returns a CoreOS ignition file for the host. Otherwise, returns a RHEL attach script",
},
"labels": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -126,51 +131,72 @@ func dataSourceIBMSatelliteAttachHostScriptRead(d *schema.ResourceData, meta int
}
}
scriptDir, _ = filepath.Abs(scriptDir)
scriptPath := filepath.Join(scriptDir, "addHost.sh")
var scriptPath string

//Generate script
createRegOptions := &kubernetesserviceapiv1.AttachSatelliteHostOptions{}
createRegOptions.Controller = locData.ID
createRegOptions.Labels = labels

//check to see if host attach is CoreOS or RHEL
var host_os string
var coreos_enabled bool
if _, ok := d.GetOk("coreos_host"); ok {
coreos_enabled = d.Get("coreos_host").(bool)
if coreos_enabled {
host_os = "RHCOS"
createRegOptions.OperatingSystem = &host_os
scriptPath = filepath.Join(scriptDir, "addHost.ign")
}
} else {
coreos_enabled = false
host_os = "RHEL"
createRegOptions.OperatingSystem = &host_os
scriptPath = filepath.Join(scriptDir, "addHost.sh")
}

resp, err := satClient.AttachSatelliteHost(createRegOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Generating Satellite Registration Script: %s\n%s", err, resp)
}

lines := strings.Split(string(resp), "\n")
for i, line := range lines {
if strings.Contains(line, "API_URL=") {
i = i + 1
if script, ok := d.GetOk("custom_script"); ok {
lines[i] = script.(string)
} else {
if strings.ToLower(hostProvider) == "aws" {
lines[i] = "yum update -y\nyum-config-manager --enable '*'\nyum repolist all\nyum install container-selinux -y"
} else if strings.ToLower(hostProvider) == "ibm" {
lines[i] = `subscription-manager refresh

//if this is a RHEL host, continue with custom script
if !coreos_enabled {
for i, line := range lines {
if strings.Contains(line, "API_URL=") {
i = i + 1
if script, ok := d.GetOk("custom_script"); ok {
lines[i] = script.(string)
} else {
if strings.ToLower(hostProvider) == "aws" {
lines[i] = "yum update -y\nyum-config-manager --enable '*'\nyum repolist all\nyum install container-selinux -y"
} else if strings.ToLower(hostProvider) == "ibm" {
lines[i] = `subscription-manager refresh
subscription-manager repos --enable rhel-server-rhscl-7-rpms
subscription-manager repos --enable rhel-7-server-optional-rpms
subscription-manager repos --enable rhel-7-server-rh-common-rpms
subscription-manager repos --enable rhel-7-server-supplementary-rpms
subscription-manager repos --enable rhel-7-server-extras-rpms`
} else if strings.ToLower(hostProvider) == "azure" {
lines[i] = fmt.Sprintf(`yum update --disablerepo=* --enablerepo="*microsoft*" -y
} else if strings.ToLower(hostProvider) == "azure" {
lines[i] = fmt.Sprintf(`yum update --disablerepo=* --enablerepo="*microsoft*" -y
yum-config-manager --enable '*'
yum repolist all
yum install container-selinux -y
`)
} else if strings.ToLower(hostProvider) == "google" {
lines[i] = fmt.Sprintf(`yum update --disablerepo=* --enablerepo="*" -y
} else if strings.ToLower(hostProvider) == "google" {
lines[i] = fmt.Sprintf(`yum update --disablerepo=* --enablerepo="*" -y
yum repolist all
yum install container-selinux -y
yum install subscription-manager -y
`)
} else {
lines[i] = "subscription-manager refresh\nyum update -y\n"
} else {
lines[i] = "subscription-manager refresh\nyum update -y\n"
}
}
}

}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,37 @@ data "ibm_satellite_attach_host_script" "script" {
host_provider = "ibm"
}`, locationName)
}

//test coreos-enabled locations
func TestAccIBMSatelliteAttachHostScriptDataSourceBasicCoreos(t *testing.T) {
locationName := fmt.Sprintf("tf-satellitelocation-coreos-%d", acctest.RandIntRange(10, 100))
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMSatelliteAttachHostScriptDataSourceConfigCoreos(locationName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.ibm_satellite_attach_host_script.script", "host_provider", "ibm"),
),
},
},
})
}

func testAccCheckIBMSatelliteAttachHostScriptDataSourceConfigCoreos(locationName string) string {
return fmt.Sprintf(`
resource "ibm_satellite_location" "testacc_satellite" {
location = "%s"
managed_from = "wdc04"
coreos_enabled = true
zones = ["us-east-1", "us-east-2", "us-east-3"]
}
data "ibm_satellite_attach_host_script" "script" {
location = ibm_satellite_location.testacc_satellite.id
labels = ["env:prod"]
coreos_host = true
host_provider = "ibm"
}`, locationName)
}
6 changes: 3 additions & 3 deletions website/docs/d/satellite_attach_host_script.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ EOF
## Argument reference
Review the argument references that you can specify for your data source.

- `custom_script` - (Optional, String) The custom script that has to be appended to generated host script file. Either `custom_script` or `host_provider` is required. This `custom_script` will be appended to the downloaded host attach script. Find custom scripts for respective cloud providers [aws](https://cloud.ibm.com/docs/satellite?topic=satellite-aws#aws-host-attach), [google](https://cloud.ibm.com/docs/satellite?topic=satellite-gcp#gcp-host-attach), [azure](https://cloud.ibm.com/docs/satellite?topic=satellite-azure#azure-host-attach), [ibm](https://cloud.ibm.com/docs/satellite?topic=satellite-ibm#ibm-host-attach).
- `coreos_host` = (Optional, Bool) True if attaching a CoreOS host to a CoreOS-enabled location. Host attach script will be in ignition file format. If attaching a RHEL host to a location, then the value is false.
- `custom_script` - (Optional, String) RHEL hosts only. The custom script that has to be appended to generated host script file. Either `custom_script` or `host_provider` is required. This `custom_script` will be appended to the downloaded host attach script. Find custom scripts for respective cloud providers [aws](https://cloud.ibm.com/docs/satellite?topic=satellite-aws#aws-host-attach), [google](https://cloud.ibm.com/docs/satellite?topic=satellite-gcp#gcp-host-attach), [azure](https://cloud.ibm.com/docs/satellite?topic=satellite-azure#azure-host-attach), [ibm](https://cloud.ibm.com/docs/satellite?topic=satellite-ibm#ibm-host-attach).
- `location` - (Required, String) The name or ID of the Satellite location.
- `host_provider` - (Optional, String) The name of host provider, such as `ibm`, `aws` or `azure`.
- `labels` - (Optional, Strings) The key-value pairs to label the host, such as `cpu=4` to describe the host capabilities.
Expand All @@ -62,5 +63,4 @@ In addition to the argument reference list, you can access the following attribu

- `id` - The unique identifier of the location.
- `script_path` - (String) Directory path to store the generated script.
- `host_script` - (String) The raw content of the script file that was read.

- `host_script` - (String) The raw content of the script file that was read.

0 comments on commit e5cb049

Please sign in to comment.