@@ -12,6 +12,7 @@ import (
1212
1313 "github.com/render-oss/cli/internal/testids"
1414 "github.com/render-oss/cli/pkg/client"
15+ "github.com/render-oss/cli/pkg/pointers"
1516)
1617
1718// queryListValues returns all values for key, splitting each occurrence on
@@ -86,6 +87,28 @@ func (kv *KVResource) nextError() (int, bool) {
8687 return status , true
8788}
8889
90+ // PostgresResource holds Postgres state and error injection for the fake server.
91+ // Tests can assert against Instances.
92+ type PostgresResource struct {
93+ Resource [* client.PostgresDetail ]
94+ errorQueue []int
95+ }
96+
97+ // RespondWith queues an HTTP status code to return on the next Postgres
98+ // operation handled by the fake server. The queue is drained in FIFO order.
99+ func (pg * PostgresResource ) RespondWith (status int ) {
100+ pg .errorQueue = append (pg .errorQueue , status )
101+ }
102+
103+ func (pg * PostgresResource ) nextError () (int , bool ) {
104+ if len (pg .errorQueue ) == 0 {
105+ return 0 , false
106+ }
107+ status := pg .errorQueue [0 ]
108+ pg .errorQueue = pg .errorQueue [1 :]
109+ return status , true
110+ }
111+
89112// NewOwner returns an Owner with sensible defaults for any zero-value fields.
90113func NewOwner (o client.Owner ) client.Owner {
91114 if o .Id == "" {
@@ -186,6 +209,7 @@ type Server struct {
186209 Projects * Resource [client.Project ]
187210 Environments * Resource [client.Environment ]
188211 KV * KVResource
212+ Postgres * PostgresResource
189213}
190214
191215// ownerByID returns the Owner with the given ID from the seeded owners. The
@@ -231,6 +255,7 @@ func NewServer(t *testing.T) *Server {
231255 Projects : & Resource [client.Project ]{},
232256 Environments : & Resource [client.Environment ]{},
233257 KV : & KVResource {},
258+ Postgres : & PostgresResource {},
234259 }
235260
236261 mux := http .NewServeMux ()
@@ -593,6 +618,73 @@ func NewServer(t *testing.T) *Server {
593618 w .WriteHeader (http .StatusNotFound )
594619 })
595620
621+ // POST /postgres — create a new Postgres instance.
622+ // Tests can assert against s.Postgres.Instances.
623+ mux .HandleFunc ("POST /postgres" , func (w http.ResponseWriter , r * http.Request ) {
624+ record (r )
625+
626+ var body client.CreatePostgresJSONRequestBody
627+ if err := json .NewDecoder (r .Body ).Decode (& body ); err != nil {
628+ w .WriteHeader (http .StatusBadRequest )
629+ return
630+ }
631+ if status , hasError := s .Postgres .nextError (); hasError {
632+ w .WriteHeader (status )
633+ return
634+ }
635+
636+ owner , ok := s .ownerByID (body .OwnerId )
637+ if ! ok {
638+ w .WriteHeader (http .StatusNotFound )
639+ return
640+ }
641+
642+ region := pointers .ValueOrDefault (body .Region , client .Oregon )
643+ databaseName := pointers .ValueOrDefault (body .DatabaseName , body .Name + "_db" )
644+ databaseUser := pointers .ValueOrDefault (body .DatabaseUser , "appuser" )
645+
646+ ipAllowList := []client.CidrBlockAndDescription {}
647+ if body .IpAllowList != nil {
648+ ipAllowList = * body .IpAllowList
649+ }
650+
651+ replicas := client.ReadReplicas {}
652+ if body .ReadReplicas != nil {
653+ for _ , r := range * body .ReadReplicas {
654+ replicas = append (replicas , client.ReadReplica {
655+ Id : testids .RandomPostgresID (),
656+ Name : r .Name ,
657+ })
658+ }
659+ }
660+
661+ id := testids .RandomPostgresID ()
662+ pg := & client.PostgresDetail {
663+ Id : id ,
664+ Name : body .Name ,
665+ Plan : body .Plan ,
666+ Version : body .Version ,
667+ Region : region ,
668+ Owner : owner ,
669+ Status : client .DatabaseStatusCreating ,
670+ DatabaseName : databaseName ,
671+ DatabaseUser : databaseUser ,
672+ DiskSizeGB : body .DiskSizeGB ,
673+ DiskAutoscalingEnabled : pointers .ValueOrDefault (body .EnableDiskAutoscaling , false ),
674+ HighAvailabilityEnabled : pointers .ValueOrDefault (body .EnableHighAvailability , false ),
675+ EnvironmentId : body .EnvironmentId ,
676+ IpAllowList : ipAllowList ,
677+ ReadReplicas : replicas ,
678+ ParameterOverrides : body .ParameterOverrides ,
679+ DashboardUrl : "https://dashboard.render.com/d/" + id ,
680+ CreatedAt : time .Now (),
681+ UpdatedAt : time .Now (),
682+ }
683+
684+ s .Postgres .Instances = append (s .Postgres .Instances , pg )
685+ writeJSON (w , http .StatusCreated , pg )
686+ })
687+
596688 s .server = httptest .NewServer (mux )
597689 t .Cleanup (s .server .Close )
598690 return s
0 commit comments