|
| 1 | +# Code Engine Go SDK Example |
| 2 | + |
| 3 | +## Running example_v2.go |
| 4 | + |
| 5 | +To run the example, create a Code Engine project from the Console or Code Engine CLI, and run the following commands from this directory: |
| 6 | +1. `export CE_API_KEY=<Your IBM Cloud API key>` |
| 7 | +2. `export CE_PROJECT_REGION=<The region (e.g. 'us-south') of your Code Engine project>` |
| 8 | +3. `export CE_DOMAIN_MAPPING_NAME=<The name of your domain>` |
| 9 | +4. `export CE_TLS_CERT_FILE_PATH=<The path to your TLS certificate file>` |
| 10 | +5. `export CE_TLS_KEY_FILE_PATH=<The path to your TLS key file>` |
| 11 | +6. `go run example_v2.go` |
| 12 | + |
| 13 | +## How-to |
| 14 | + |
| 15 | +### Set up an authenticator |
| 16 | +```go |
| 17 | +authenticator := &core.IamAuthenticator{ |
| 18 | + ApiKey: os.Getenv("CE_API_KEY"), |
| 19 | + ClientId: "bx", |
| 20 | + ClientSecret: "bx", |
| 21 | + URL: "https://iam.cloud.ibm.com", |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +### Set up a Code Engine client |
| 26 | +```go |
| 27 | +codeEngineServiceOptions := &codeenginev2.CodeEngineV2Options{ |
| 28 | + Authenticator: authenticator, |
| 29 | + URL: "https://api." + os.Getenv("CE_PROJECT_REGION") + ".codeengine.cloud.ibm.com/v2", |
| 30 | +} |
| 31 | +codeEngineService, err := codeenginev2.NewCodeEngineV2UsingExternalConfig(codeEngineServiceOptions) |
| 32 | +``` |
| 33 | + |
| 34 | +### Create a Code Engine project |
| 35 | +```go |
| 36 | +projectName := "my-project" |
| 37 | +createdProject, _, err := codeEngineService.CreateProject(&codeenginev2.CreateProjectOptions{ |
| 38 | + Name: &projectName, |
| 39 | +}) |
| 40 | +``` |
| 41 | + |
| 42 | +### Create a Code Engine application |
| 43 | +```go |
| 44 | +createAppOpts := codeEngineService.NewCreateAppOptions( |
| 45 | + *createdProject.ID, |
| 46 | + "icr.io/codeengine/helloworld", |
| 47 | + "my-app", |
| 48 | +) |
| 49 | +createdApp, _, err := codeEngineService.CreateApp(createAppOpts) |
| 50 | +``` |
| 51 | + |
| 52 | +### Create a Code Engine TLS secret |
| 53 | +```go |
| 54 | +createTLSSecretOpts := codeEngineService.NewCreateSecretOptions( |
| 55 | + *createdProject.ID, |
| 56 | + "tls", |
| 57 | + "my-tls-secret", |
| 58 | +) |
| 59 | + |
| 60 | +tlsCert, _ := os.ReadFile(os.Getenv("CE_TLS_CERT_FILE_PATH")) |
| 61 | +tlsKey, _ := os.ReadFile(os.Getenv("CE_TLS_KEY_FILE_PATH")) |
| 62 | + |
| 63 | +createTLSSecretOpts.Data = &codeenginev2.SecretDataTLSSecretData{ |
| 64 | + TlsCert: core.StringPtr(string(tlsCert)), |
| 65 | + TlsKey: core.StringPtr(string(tlsKey)), |
| 66 | +} |
| 67 | +createdTLSSecret, _, err := codeEngineService.CreateSecret(createTLSSecretOpts) |
| 68 | +``` |
| 69 | + |
| 70 | +### Create a Code Engine domain mapping |
| 71 | +```go |
| 72 | +domainMappingName := os.Getenv("CE_DOMAIN_MAPPING_NAME") |
| 73 | +appComponentRef := &codeenginev2.ComponentRef{ |
| 74 | + Name: createdApp.Name, |
| 75 | + ResourceType: core.StringPtr("app_v2"), |
| 76 | +} |
| 77 | + |
| 78 | +createDomainMappingOpts := codeEngineService.NewCreateDomainMappingOptions( |
| 79 | + *createdProject.ID, |
| 80 | + appComponentRef, |
| 81 | + domainMappingName, |
| 82 | + *createdTLSSecret.Name, |
| 83 | +) |
| 84 | +createdDomainMapping, _, err := codeEngineService.CreateDomainMapping(createDomainMappingOpts) |
| 85 | +``` |
0 commit comments