github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/examples/schematics/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	bluemix "github.com/IBM-Cloud/bluemix-go"
     8  
     9  	"github.com/IBM-Cloud/bluemix-go/session"
    10  
    11  	sch "github.com/IBM-Cloud/bluemix-go/api/schematics"
    12  	"github.com/IBM-Cloud/bluemix-go/trace"
    13  )
    14  
    15  func main() {
    16  	c := new(bluemix.Config)
    17  
    18  	var workspaceID string
    19  	var templateid string
    20  	fmt.Print("Enter Workspace ID: ")
    21  	fmt.Scanln(&workspaceID)
    22  	fmt.Print("Enter Template ID: ")
    23  	fmt.Scanln(&templateid)
    24  
    25  	trace.Logger = trace.NewLogger("true")
    26  	var payload = sch.Payload{
    27  		Name:        "bbb",
    28  		Type:        []string{"terraform-v1.0"},
    29  		Description: "terraform workspace",
    30  		Tags:        []string{"department:HR", "application:compensation", "environment:staging"},
    31  		WorkspaceStatus: sch.WorkspaceStatus{
    32  			Frozen: true,
    33  		},
    34  		TemplateRepo: sch.TemplateRepo{
    35  			URL: "https://github.com/ptaube/tf_cloudless_sleepy",
    36  		},
    37  		TemplateRef: "ibm-open-liberty-2ae855ce3ca4",
    38  		TemplateData: []sch.TemplateData{
    39  			{
    40  				Folder: ".",
    41  				Type:   "terraform-v1.0",
    42  
    43  				Variablestore: []sch.Variablestore{
    44  					{
    45  						Name:        "sample_var",
    46  						Secure:      false,
    47  						Value:       "THIS IS IBM CLOUD TERRAFORM CLI DEMO",
    48  						Description: "Description of sample_var",
    49  					},
    50  					{
    51  						Name:  "sleepy_time",
    52  						Value: "15",
    53  					},
    54  				},
    55  			},
    56  		},
    57  	}
    58  
    59  	sess, err := session.New(c)
    60  	if err != nil {
    61  		log.Fatal(err)
    62  	}
    63  	schClient, err := sch.New(sess)
    64  	if err != nil {
    65  		log.Fatal(err)
    66  	}
    67  	schAPI := schClient.Workspaces()
    68  	//Get the workspace
    69  	works, err := schAPI.GetWorkspaceByID(workspaceID)
    70  	if err != nil {
    71  		log.Fatal(err)
    72  	}
    73  
    74  	fmt.Println("\nThe workspace info= ", works)
    75  
    76  	state, err := schAPI.GetStateStore(workspaceID, templateid)
    77  	if err != nil {
    78  		log.Fatal(err)
    79  	}
    80  	statestr := fmt.Sprintf("%v", state)
    81  	fmt.Println("The state info= ", statestr)
    82  
    83  	out, err := schAPI.GetOutputValues(workspaceID)
    84  	if err != nil {
    85  		log.Fatal(err)
    86  	}
    87  
    88  	fmt.Println("The output info= ", out)
    89  	items := make(map[string]interface{})
    90  
    91  	for _, feilds := range out {
    92  		if feilds.TemplateID == "653f60a4-f64f-41" {
    93  			output := feilds.Output
    94  
    95  			for _, value := range output {
    96  				for key, val := range value {
    97  					val2 := val.Value
    98  					items[key] = val2
    99  
   100  				}
   101  			}
   102  		}
   103  	}
   104  
   105  	fmt.Println("\nThe array info= ", items)
   106  
   107  	cdata, err := schAPI.CreateWorkspace(payload)
   108  
   109  	fmt.Println("\ndata=", cdata)
   110  	fmt.Println("\nid=", cdata.ID)
   111  
   112  }