Skip to content

Instantly share code, notes, and snippets.

@jonmorehouse
Created September 29, 2022 23:00
Show Gist options
  • Save jonmorehouse/15fdd83532ba5ced46927f6ee70810ee to your computer and use it in GitHub Desktop.
Save jonmorehouse/15fdd83532ba5ced46927f6ee70810ee to your computer and use it in GitHub Desktop.
package cmd
import (
"context"
"fmt"
"log"
"github.com/hashicorp/waypoint/pkg/server/gen"
"github.com/powertoolsdev/go-common/config"
"github.com/powertoolsdev/go-common/shortid"
"github.com/spf13/cobra"
"github.com/powertoolsdev/temporal-workers/pkg/waypoint"
)
var playgroundCmd = &cobra.Command{
Use: "playground",
Short: "Run the playground command",
Run: playgroundRun,
}
//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(playgroundCmd)
}
func playgroundRun(cmd *cobra.Command, args []string) {
var cfg Config
if len(args) > 0 {
log.Fatalf("no arguments accepted")
}
if err := config.LoadInto(cmd.Flags(), &cfg); err != nil {
panic(fmt.Sprintf("failed to load config: %s", err))
}
ctx := context.Background()
componentName := "httpbin"
orgLongID := "2d286c50-611f-4f1b-a2a1-f0434673c2d2"
orgID, err := shortid.ParseString(orgLongID)
if err != nil {
log.Fatalf("unable to parse installID to shortID: %s", err)
}
installID := "10elfrb5y37gv22a1qc9bxh74g"
orgServerAddr := waypoint.DefaultOrgServerAddress("stage.nuon.co", orgID)
provider := waypoint.NewProvider()
client, err := provider.GetOrgWaypointClient(ctx, "default", orgID, orgServerAddr)
if err != nil {
log.Fatalf("unable to get waypoint org client: %s", err)
}
fmt.Println("here")
// get runner
project, err := client.DestroyProject(ctx, &gen.DestroyProjectRequest{
Project: &gen.Ref_Project{
Project: installID,
},
})
if err != nil {
log.Fatalf("unable to get install project: %s", err)
}
fmt.Println(project)
projectReq := &gen.UpsertProjectRequest{
Project: &gen.Project{
Name: installID,
RemoteEnabled: true,
DataSource: &gen.Job_DataSource{
Source: &gen.Job_DataSource_Git{
Git: &gen.Job_Git{
Url: "https://github.com/jonmorehouse/empty",
},
},
},
DataSourcePoll: &gen.Project_Poll{
Enabled: false,
},
},
}
_, err = client.UpsertProject(ctx, projectReq)
if err != nil {
log.Fatalf("unable to upsert project: %s", err)
}
// upsert app
appReq := &gen.UpsertApplicationRequest{
Project: &gen.Ref_Project{
Project: installID,
},
Name: componentName,
}
resp, err := client.UpsertApplication(ctx, appReq)
if err != nil {
log.Fatalf("unable to create application for component: %s", err)
}
fmt.Println(resp.String())
// upsertDeployment
deployReq := &gen.UpsertDeploymentRequest{
Deployment: &gen.Deployment{
Workspace: &gen.Ref_Workspace{
Workspace: installID,
},
Application: &gen.Ref_Application{
Application: componentName,
Project: installID,
},
},
}
deployResp, err := client.UpsertDeployment(ctx, deployReq)
if err != nil {
log.Fatalf("unable to create deployment: %s", err)
}
dep := deployResp.Deployment
fmt.Println(dep.JobId)
job := &gen.Job{
TargetRunner: &gen.Ref_Runner{
Target: &gen.Ref_Runner_Id{Id: &gen.Ref_RunnerId{Id: installID}},
},
// Labels: c.labels,
// Variables: c.variables,
Workspace: &gen.Ref_Workspace{
Workspace: installID,
},
Application: &gen.Ref_Application{
Project: installID,
},
DataSourceOverrides: map[string]string{},
Operation: &gen.Job_Deploy{
Deploy: &gen.Job_DeployOp{
Artifact: &gen.PushedArtifact{},
},
},
}
queueResp, err := client.QueueJob(ctx, &gen.QueueJobRequest{
Job: job,
ExpiresIn: "30m",
})
if err != nil {
log.Fatalf("error queueing job %s:", err)
}
jobID := queueResp.JobId
fmt.Println(jobID)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment