@@ -434,68 +434,50 @@ type QueryAPI struct {
434434 context context.Context
435435}
436436
437- func (a QueryAPI ) buildPath (path ... string ) string {
438- out := "/preview/sql/queries"
439- if len (path ) == 1 {
440- out = out + "/" + strings .Join (path , "/" )
441- }
442- return out
443- }
444-
445437// Create ...
446- func (a QueryAPI ) Create (q * api.Query ) (* api.Query , error ) {
447- var qp api.Query
448- err := a .client .Post (a .context , a .buildPath (), q , & qp )
438+ func (a QueryAPI ) Create (q * api.Query ) error {
439+ err := a .client .Post (a .context , "/preview/sql/queries" , q , & q )
449440 if err != nil {
450- return nil , err
441+ return err
451442 }
452443
453444 // New queries are created with a table visualization by default.
454445 // We don't manage that visualization here, so immediately remove it.
455- if len (qp .Visualizations ) > 0 {
456- for _ , rv := range qp .Visualizations {
457- var v api.Visualization
458- err = json .Unmarshal (rv , & v )
459- if err != nil {
460- return nil , err
461- }
462- // TODO
463- // err = a.DeleteVisualization(&v)
464- // if err != nil {
465- // return nil, err
466- // }
446+ for _ , rv := range q .Visualizations {
447+ var v api.Visualization
448+ err = json .Unmarshal (rv , & v )
449+ if err != nil {
450+ return err
467451 }
468- qp .Visualizations = []json.RawMessage {}
452+ // TODO
453+ // err = a.DeleteVisualization(&v)
454+ // if err != nil {
455+ // return nil, err
456+ // }
469457 }
470-
471- return & qp , err
458+ q . Visualizations = []json. RawMessage {}
459+ return nil
472460}
473461
474462// Read ...
475- func (a QueryAPI ) Read (q * api. Query ) (* api.Query , error ) {
476- var qp api.Query
477- err := a .client .Get (a .context , a . buildPath ( q . ID ), nil , & qp )
463+ func (a QueryAPI ) Read (queryID string ) (* api.Query , error ) {
464+ var q api.Query
465+ err := a .client .Get (a .context , fmt . Sprintf ( "/preview/sql/queries/%s" , queryID ), nil , & q )
478466 if err != nil {
479467 return nil , err
480468 }
481469
482- return & qp , nil
470+ return & q , nil
483471}
484472
485473// Update ...
486- func (a QueryAPI ) Update (q * api.Query ) (* api.Query , error ) {
487- var qp api.Query
488- err := a .client .Post (a .context , a .buildPath (q .ID ), q , & qp )
489- if err != nil {
490- return nil , err
491- }
492-
493- return & qp , nil
474+ func (a QueryAPI ) Update (queryID string , q * api.Query ) error {
475+ return a .client .Post (a .context , fmt .Sprintf ("/preview/sql/queries/%s" , queryID ), q , nil )
494476}
495477
496478// Delete ...
497- func (a QueryAPI ) Delete (q * api. Query ) error {
498- return a .client .Delete (a .context , a . buildPath ( q . ID ), nil )
479+ func (a QueryAPI ) Delete (queryID string ) error {
480+ return a .client .Delete (a .context , fmt . Sprintf ( "/preview/sql/queries/%s" , queryID ), nil )
499481}
500482
501483// ResourceQuery ...
@@ -541,29 +523,24 @@ func ResourceQuery() *schema.Resource {
541523 return err
542524 }
543525
544- aqNew , err : = NewQueryAPI (ctx , c ).Create (aq )
526+ err = NewQueryAPI (ctx , c ).Create (aq )
545527 if err != nil {
546528 return err
547529 }
548530
549531 // No need to set anything because the resource is going to be
550532 // read immediately after being created.
551- data .SetId (aqNew .ID )
533+ data .SetId (aq .ID )
552534 return nil
553535 },
554536 Read : func (ctx context.Context , data * schema.ResourceData , c * common.DatabricksClient ) error {
555- var q QueryEntity
556- aq , err := q .toAPIObject (s , data )
537+ aq , err := NewQueryAPI (ctx , c ).Read (data .Id ())
557538 if err != nil {
558539 return err
559540 }
560541
561- aqNew , err := NewQueryAPI (ctx , c ).Read (aq )
562- if err != nil {
563- return err
564- }
565-
566- return q .fromAPIObject (aqNew , s , data )
542+ var q QueryEntity
543+ return q .fromAPIObject (aq , s , data )
567544 },
568545 Update : func (ctx context.Context , data * schema.ResourceData , c * common.DatabricksClient ) error {
569546 var q QueryEntity
@@ -572,23 +549,10 @@ func ResourceQuery() *schema.Resource {
572549 return err
573550 }
574551
575- _ , err = NewQueryAPI (ctx , c ).Update (aq )
576- if err != nil {
577- return err
578- }
579-
580- // No need to set anything because the resource is going to be
581- // read immediately after being created.
582- return nil
552+ return NewQueryAPI (ctx , c ).Update (data .Id (), aq )
583553 },
584554 Delete : func (ctx context.Context , data * schema.ResourceData , c * common.DatabricksClient ) error {
585- var q QueryEntity
586- aq , err := q .toAPIObject (s , data )
587- if err != nil {
588- return err
589- }
590-
591- return NewQueryAPI (ctx , c ).Delete (aq )
555+ return NewQueryAPI (ctx , c ).Delete (data .Id ())
592556 },
593557 Schema : s ,
594558 }.ToResource ()
0 commit comments