bicep icon indicating copy to clipboard operation
bicep copied to clipboard

Paste as Bicep

Open aelij opened this issue 3 years ago • 6 comments

Is your feature request related to a problem? Please describe. There are many ARM samples out there. Even on MS docs, most samples are still JSON. Copying and pasting pieces from samples is a very common way to build a template. Right now the only option for Bicep is to download a template file, decompile it, and copy and paste the relevant part.

Describe the solution you'd like Add a 'Paste as Bicep' command to the VS Code extension that will convert the clipboard contents from JSON to Bicep and paste it.

I would not expect such a command to result in working Bicep code, e.g. missing vars/parameters are acceptable.

aelij avatar Mar 07 '21 14:03 aelij

It would be a cool feature, particularly for code snippets we don't own. Though, for all the content we do own, our goal is to provide a bicep equivalent for every JSON snippet/sample.

alex-frankel avatar Mar 08 '21 17:03 alex-frankel

Nice milestone name :)

aelij avatar Mar 09 '21 10:03 aelij

@aelij I've created a PowerShell function that converts JSON to Bicep format, you might find it useful. https://github.com/StefanIvemo/BicepPowerShell/blob/main/Docs/Help/Convert-JsonToBicep.md.

StefanIvemo avatar May 07 '21 13:05 StefanIvemo

@StefanIvemo Thanks! Will check it out.

aelij avatar May 09 '21 05:05 aelij

This has been mentioned in a few comments since last year. Taking a look at this sort of customer feedback and the fact that Bicep decompile has come up in interviews, is one of our top 5 commands used in Az Bicep CLI. This is worth investing in as we continue to move folks over from ARM to Bicep.

@StephenWeatherford, I'm interested to know, do you believe there is an easy way to detect on someone's clipboard that they are holding ARM text?

After copying ARM text something like this

"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2019-04-01",
    "name": "mystorageaccount",
    "location": "westus",
    "sku": {
      "name": "Standard_LRS"
    },
    "kind": "StorageV2",
    "properties": {}
  }
]

I propose the user should be able to right click in a Bicep file and see this: image

ucheNkadiCode avatar Mar 23 '22 22:03 ucheNkadiCode

I'm interested to know, do you believe there is an easy way to detect on someone's clipboard that they are holding ARM text?

Yes, with reasonable but not perfect accuracy.

StephenWeatherford avatar Mar 28 '22 17:03 StephenWeatherford

Implementation issues and progress

  • [x] P1: #9218
  • [x] P1: #9219
    • PR almost ready to review: https://github.com/Azure/bicep/pull/9519
  • [x] Instability in test: pasteAsBicep should convert pasted list of resources
    • https://github.com/Azure/bicep/actions/runs/3897643094/jobs/6655538552
    • Handled by #9703
  • [ ] P1: Don't eat unconverted JSON at end of input
  • [x] P1: Sporadic: Internal Error - System.ArgumentException: The specified line number is not valid. (Haven't seen this for a while, does it still repro?)
  • [x] #9509
  • [x] #9510
  • [x] https://github.com/Azure/bicep/issues/9847 See if can create new files in the editor if additional files created (modules from nested templates)
  • [x] should only get telemetry when not queryCanPaste
  • [x] e2e typescript tests
  • [x] review telemetry
  • [x] what if resources list is from a subscription scoped template?
  • [x] What if the string being pasted is already valid Bicep? Do we convert it (which the user will see as a formatting change) or do we skip the conversion? A: yes
  • [ ] Allow brackets missing at end of input

Dev Comments

  • I assume we want the following functionality:
    • paste an entire ARM template
    • paste an individual resource or subset of resources
    • paste an object and have double quotes converted to single quotes/etc
    • [ ] What about output from az resource list? (I.e. same as Insert as Resource uses but gets directly from Azure). Example:
{
  "id": "/subscriptions/964df7ca-3ba4-48b6-a695-1ed9db5723f8/resourceGroups/1-77f5bf5b-playground-sandbox/providers/Microsoft.Compute/virtualMachines/untagged-vm/providers/Microsoft.Resources/tags/default",
  "name": "default",
  "properties": {
    "tags": {
      "project": "cloudlinux"
    }
  },
  "resourceGroup": "1-77f5bf5b-playground-sandbox",
  "type": "Microsoft.Resources/tags"
}

Issues

  • [x] A big issue will be what missing pieces we can tolerate, e.g.:
    • Missing params and variables shouldn't be a big problem (but will have to make changes - currently the decompiler fails on such inputs)
    • Missing resource references?
    • Missing user-functions
      • Already not supported
  • [x] What if it includes a nested template (and thus requires creating additional files)?
  • [x] Allow invalid JSON
    • [x] e.g. brackets not closed at end
    • [x] extra brackets
    • [x] comma at end

Examples

  • https://learn.microsoft.com/en-us/azure/templates/microsoft.storage/storageaccounts?pivots=deployment-language-arm-template#resource-format-1

StephenWeatherford avatar Oct 12 '22 19:10 StephenWeatherford

@aelij @StefanIvemo Please turn on the following options to try this out and give us feedback! image

StephenWeatherford avatar Feb 08 '23 19:02 StephenWeatherford

Just wanted to say this feature is VERY useful when converting NSG JSON to Bicep 🎉

This is a huge quality of life improvement from POV, thank you!

coolhome avatar Feb 28 '23 22:02 coolhome

@coolhome Great to hear, thanks! BTW, the experimental flag is being removed next release, so it will be default behavior.

StephenWeatherford avatar Feb 28 '23 23:02 StephenWeatherford