github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql_client/director_client.go (about) 1 package graphqlclient 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/kyma-incubator/compass/components/director/pkg/graphql/graphqlizer" 8 9 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 10 11 gcli "github.com/machinebox/graphql" 12 "github.com/pkg/errors" 13 ) 14 15 // GraphQLClient expects graphql implementation 16 // 17 //go:generate mockery --name=GraphQLClient --output=automock --outpkg=automock --case=underscore --disable-version-string 18 type GraphQLClient interface { 19 Run(context.Context, *gcli.Request, interface{}) error 20 } 21 22 // Director is an GraphQLClient implementation 23 type Director struct { 24 client GraphQLClient 25 } 26 27 // NewDirector creates new director with given client 28 func NewDirector(client GraphQLClient) *Director { 29 return &Director{ 30 client: client, 31 } 32 } 33 34 // WriteTenants makes graphql query for tenant creation 35 func (d *Director) WriteTenants(ctx context.Context, tenants []graphql.BusinessTenantMappingInput) error { 36 var res map[string]interface{} 37 38 gqlizer := graphqlizer.Graphqlizer{} 39 in, err := gqlizer.WriteTenantsInputToGQL(tenants) 40 if err != nil { 41 return errors.Wrap(err, "while creating tenants input") 42 } 43 tenantsQuery := fmt.Sprintf("mutation { writeTenants(in:[%s])}", in) 44 gRequest := gcli.NewRequest(tenantsQuery) 45 if err := d.client.Run(ctx, gRequest, &res); err != nil { 46 return errors.Wrap(err, "while executing gql query") 47 } 48 return nil 49 } 50 51 // DeleteTenants makes graphql query for tenant deletion 52 func (d *Director) DeleteTenants(ctx context.Context, tenants []graphql.BusinessTenantMappingInput) error { 53 var res map[string]interface{} 54 55 gqlizer := graphqlizer.Graphqlizer{} 56 in, err := gqlizer.DeleteTenantsInputToGQL(tenants) 57 if err != nil { 58 return errors.Wrap(err, "while creating tenants input") 59 } 60 tenantsQuery := fmt.Sprintf("mutation { deleteTenants(in:[%s])}", in) 61 gRequest := gcli.NewRequest(tenantsQuery) 62 if err := d.client.Run(ctx, gRequest, &res); err != nil { 63 return errors.Wrap(err, "while executing gql query") 64 } 65 return nil 66 } 67 68 // UpdateTenant makes graphql query for tenant update 69 func (d *Director) UpdateTenant(ctx context.Context, id string, tenant graphql.BusinessTenantMappingInput) error { 70 var res map[string]interface{} 71 72 fieldProvider := graphqlizer.GqlFieldsProvider{} 73 gqlizer := graphqlizer.Graphqlizer{} 74 in, err := gqlizer.UpdateTenantsInputToGQL(tenant) 75 if err != nil { 76 return errors.Wrap(err, "while creating tenants input") 77 } 78 tenantsQuery := fmt.Sprintf(`mutation { updateTenant(id: "%s", in:%s) { %s }}`, id, in, fieldProvider.ForTenant()) 79 gRequest := gcli.NewRequest(tenantsQuery) 80 if err := d.client.Run(ctx, gRequest, &res); err != nil { 81 return errors.Wrap(err, "while executing gql query") 82 } 83 return nil 84 } 85 86 // SubscribeTenant makes graphql query tenant subscription 87 func (d *Director) SubscribeTenant(ctx context.Context, providerID, subaccountID, providerSubaccountID, consumerTenantID, region, subscriptionProviderAppName, subscriptionPayload string) error { 88 var res map[string]interface{} 89 90 subscriptionMutation := fmt.Sprintf(`mutation { subscribeTenant(providerID: "%s", subaccountID: "%s", providerSubaccountID: "%s", consumerTenantID: "%s", region: "%s", subscriptionAppName: "%s", subscriptionPayload: %q)}`, providerID, subaccountID, providerSubaccountID, consumerTenantID, region, subscriptionProviderAppName, subscriptionPayload) 91 gRequest := gcli.NewRequest(subscriptionMutation) 92 if err := d.client.Run(ctx, gRequest, &res); err != nil { 93 return errors.Wrap(err, "while executing gql mutation") 94 } 95 return nil 96 } 97 98 // UnsubscribeTenant makes graphql query tenant unsubscription 99 func (d *Director) UnsubscribeTenant(ctx context.Context, providerID, subaccountID, providerSubaccountID, consumerTenantID, region string) error { 100 var res map[string]interface{} 101 102 unsubscriptionMutation := fmt.Sprintf(`mutation { unsubscribeTenant(providerID: "%s", subaccountID: "%s", providerSubaccountID: "%s", consumerTenantID: "%s", region: "%s")}`, providerID, subaccountID, providerSubaccountID, consumerTenantID, region) 103 gRequest := gcli.NewRequest(unsubscriptionMutation) 104 if err := d.client.Run(ctx, gRequest, &res); err != nil { 105 return errors.Wrap(err, "while executing gql mutation") 106 } 107 return nil 108 }