func NewCreateViewsCmd()

in tools/mconnect/commands/views/views.go [117:167]


func NewCreateViewsCmd(factory viewCreatorFactory) *cobra.Command {
	return &cobra.Command{
		Use:   "create-views project dataset",
		Short: "Creates three views in BigQuery using Migration Center and CAST data and outputs a link to a Looker Studio report using these views.",
		Long: `Creates three views in BigQuery using Migration Center and CAST data.
Provides a link for a Looker Studio report using the 'mccastreadinesscombined_vw' view.

Views created:
	migrationcenterinfra_vw - Shows grouped asset data from Migration Center.
	castreadiness_vw - Shows data from the CAST Analysis file.
	mccastreadinesscombined_vw - Combines the two previous views. This view is also used in Looker Studio's Template.


`,
		Example: `
mconnect create-views --project=my-project-id --dataset=dataset-id	
mconnect create-views --project=my-project-id --dataset=dataset-id --force=true`,
		RunE: func(cmd *cobra.Command, args []string) error {
			ctx := context.Background()

			if len(args) != 0 {
				return messages.NoArgumentsAcceptedError{Args: args}.Error()
			}

			// Remove usage printing on error once initial parameter validations are done.
			cmd.SilenceUsage = true

			vc := factory.build(projectID, datasetID)
			viewsMetadata := []viewMetadata{
				newViewMetadata(mcViewName, "mc view description", mcQuery(projectID, datasetID)),
				newViewMetadata(castViewName, "cast view description", castQuery(projectID, datasetID)),
				newViewMetadata(combinedViewName, "combined view description", combinedQuery(projectID, datasetID)),
			}
			for _, metadata := range viewsMetadata {
				err := vc.createView(ctx, metadata)
				if err != nil {
					if gapiutil.IsErrorWithCode(err, http.StatusConflict) {
						return fmt.Errorf(messages.CreatingViewExistError{Name: metadata.name}.String())
					}

					return messages.CreatingViewError{Metadata: metadata.name, Err: err}.Error()
				}
			}

			// Prints the Looker Studio Link with the users data connected to it.
			fmt.Println(messages.LookerLinkInstruction{Link: lookerStudioLink(projectID, datasetID)})

			return nil
		},
	}
}