Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repository import: "inputs to import do not match the existing resource" #757

Open
acuteaura opened this issue Sep 4, 2024 · 1 comment
Labels
area/import An issue related to `pulumi import` or the import resource option. kind/bug Some behavior is incorrect or out of spec

Comments

@acuteaura
Copy link

acuteaura commented Sep 4, 2024

We use the automation API to create repositories based on desired state. Sometimes we need to import a repository.

At this time, we set very few properties, and want everything else to default (securityAndAnalysis already failed this. so we work around it):

repoPulumiOpts = append(repoPulumiOpts, pulumi.IgnoreChanges([]string{"securityAndAnalysis"}))

[...]

pRepoArgs := &github.RepositoryArgs{
	Name:       pulumi.StringPtr(name),
	Visibility: pulumi.StringPtr("private"),

	SecurityAndAnalysis: nil,
}

This works fine and reconciles when we create the repository through pulumi. But on importing, it will fail very nondescript.

Diagnostics:
  github:index:Repository (repository-3):
    warning: urn:pulumi:repository::ci-assistant::github:index/repository:Repository::repository-3 verification warning: Use the github_branch_default resource instead
    error: inputs to import do not match the existing resource
  pulumi:pulumi:Stack (ci-assistant-repository):
    error: update failed

This is a repository without any special settings, created via GitHub Web.

When attempting to import this resource via pulumi CLI, I get the supposedly imported properties:

        [provider=urn:pulumi:repository::ci-assistant::pulumi:providers:github::default_6_2_1::6427e8f0-81d2-4d3a-b5e8-312ac002e3c1]
        defaultBranch: "main"
        hasDownloads : true
        hasIssues    : true
        hasProjects  : true
        name         : "ci-test2"
        private      : true
        visibility   : "private"

When trying to set these when importing, it still fails. Without any hint as to what's missing:

if repo.Import {
	repoAsIs, err := github.GetRepository(ctx, name, pulumi.ID(name), nil, pulumi.Providers(githubProvider))
	if err != nil {
		return err
	}
	pRepoArgs.DefaultBranch = repoAsIs.DefaultBranch
	pRepoArgs.HasIssues = repoAsIs.HasIssues
	pRepoArgs.HasProjects = repoAsIs.HasProjects
	pRepoArgs.HasWiki = repoAsIs.HasWiki
	pRepoArgs.Visibility = repoAsIs.Visibility
}

(setting private and visibility will fail, and default branch emits warnings as seen above; oh and githubProvider is needed because pulumi via auto.EnvVars didn't pick up on app config via environment at all!)

Can you give me any pointers as to what I can do that's not stopping the entire thing and editing the state by hand (by basing it on another created by pulumi).

importing via CLI creates bricked state:

github:index:Repository repository-3 creating replacement (0s) [diff: +allowAutoMerge,allowMergeCommit,allowRebaseMerge,allowSquashMerge,archived,deleteBranchOnMerge,mergeCommitMessage,mergeCommitTitle,squashMergeCommitMessage,squashMergeCommitTitle,webCommitSignoffRequired-defaultBranch,hasDownloads,hasIssues,hasProjects,private~__defaults,protect,provider]; error: 1 error occurred:
 ++ github:index:Repository repository-3 **creating failed** [diff: +allowAutoMerge,allowMergeCommit,allowRebaseMerge,allowSquashMerge,archived,deleteBranchOnMerge,mergeCommitMessage,mergeCommitTitle,squashMergeCommitMessage,squashMergeCommitTitle,webCommitSignoffRequired-defaultBranch,hasDownloads,hasIssues,hasProjects,private~__defaults,protect,provider]; error: 1 error occurred:
    pulumi:pulumi:Stack ci-assistant-repository running error: update failed
    pulumi:pulumi:Stack ci-assistant-repository **failed** 1 error
Diagnostics:
  github:index:Repository (repository-3):
    error: 1 error occurred:
    \t* POST https://api.github.com/orgs/hmgassistanttest/repos: 422 Repository creation failed. [{Resource:Repository Field:name Code:custom Message:name already exists on this account}]

@pulumi-bot pulumi-bot added the needs-triage Needs attention from the triage team label Sep 4, 2024
@iwahbe iwahbe added kind/bug Some behavior is incorrect or out of spec area/import An issue related to `pulumi import` or the import resource option. and removed needs-triage Needs attention from the triage team labels Sep 10, 2024
@iwahbe
Copy link
Member

iwahbe commented Sep 10, 2024

Hi @acuteaura. Thanks for opening an issue and sorry for the long response time.

The way that Pulumi import works, you need to specify all fields whos value disagrees with the provider's default. Most providers don't support partially managed resources. If you run a pulumi preview, you will see a more useful warning. For example, when I tried to import this repo:

    = github:index/repository:Repository: (import) 🔒
        [id=pulumi-github]
        [urn=urn:pulumi:dev::vpc::github:index/repository:Repository::example]
        [provider=urn:pulumi:dev::vpc::pulumi:providers:github::p::cd4cda5e-de7a-48df-90fc-a69a2eda9618]
      ~ allowAutoMerge          : true => false
      ~ deleteBranchOnMerge     : true => false
      - description             : "A Pulumi package to facilitate interacting with GitHub"
      - hasDownloads            : true
      - hasIssues               : true
      - hasProjects             : true
      - hasWiki                 : true
      ~ mergeCommitMessage      : "PR_BODY" => "PR_TITLE"
      ~ mergeCommitTitle        : "PR_TITLE" => "MERGE_MESSAGE"
      ~ squashMergeCommitMessage: "PR_BODY" => "COMMIT_MESSAGES"
      ~ squashMergeCommitTitle  : "PR_TITLE" => "COMMIT_OR_PR_TITLE"
      - vulnerabilityAlerts     : true

This program worked for me:

func Main(ctx *pulumi.Context) error {
	p, err := github.NewProvider(ctx, "p", &github.ProviderArgs{
		Owner: pulumi.String("pulumi"),
	})
	_, err = github.NewRepository(ctx, "example", &github.RepositoryArgs{
		Name:                     pulumi.String("pulumi-github"),
		AllowAutoMerge:           pulumi.Bool(true),
		DeleteBranchOnMerge:      pulumi.Bool(true),
		HasDownloads:             pulumi.Bool(true),
		HasIssues:                pulumi.Bool(true),
		HasProjects:              pulumi.Bool(true),
		HasWiki:                  pulumi.Bool(true),
		VulnerabilityAlerts:      pulumi.Bool(true),
		MergeCommitMessage:       pulumi.String("PR_BODY"),
		MergeCommitTitle:         pulumi.String("PR_TITLE"),
		SquashMergeCommitMessage: pulumi.String("PR_BODY"),
		SquashMergeCommitTitle:   pulumi.String("PR_TITLE"),
		Description:              pulumi.String("A Pulumi package to facilitate interacting with GitHub"),
	},
		pulumi.Import(pulumi.ID("pulumi-github")),
		pulumi.Protect(true),
		pulumi.RetainOnDelete(true),
		pulumi.Provider(p),
	)
	return err
}

I agree that the error message on pulumi up is unusable. pulumi/pulumi#3737 is the issue tracking a better error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/import An issue related to `pulumi import` or the import resource option. kind/bug Some behavior is incorrect or out of spec
Projects
None yet
Development

No branches or pull requests

3 participants